473,385 Members | 1,347 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Reflection on Constants

Hi there, i'm new to reflection, i'm trying to retrieve a constant value,
but my code is not working:

....
public const int XXX = 5;

public void main()
{
System.Reflection.FieldInfo fi = this.GetType().GetField("XXX");
int temp = (int)fi.GetValue(this);
}
....

If i change the 'XXX' declaration to not-a-constant, the value is retrieved
successfully.
Nov 15 '05 #1
1 8772
Try out the following code snip.

namespace ConsoleApplication4

{

public class reflection

{

public const string mstr = "hello world!";

public reflection()

{

}

public static void Main()

{

System.Reflection.FieldInfo fi =
Type.GetType("ConsoleApplication4.reflection") .GetField("mstr");

string temp = (string)fi.GetValue(null);

Console.WriteLine("get string {0}", temp);

}

}

}

"Allan Rojas" <nd****@thecqgl.com> wrote in message
news:Ob*************@TK2MSFTNGP10.phx.gbl...
'Object reference not set to an instance of an object'

The FieldInfo object initialized on the first line of the 'main' method
never finds the field; after execution is still null/empty.
"jennyq" <je**********@yahoo.com> wrote in message
news:eH**************@TK2MSFTNGP12.phx.gbl...
Hi, Allan:

try to use GetValue(null) if you want to retrieve static/const
value on class.
"Allan Rojas" <nd****@thecqgl.com> wrote in message
news:uu**************@tk2msftngp13.phx.gbl...
Hi there, i'm new to reflection, i'm trying to retrieve a constant value, but my code is not working:

...
public const int XXX = 5;

public void main()
{
System.Reflection.FieldInfo fi = this.GetType().GetField("XXX");
int temp = (int)fi.GetValue(this);
}
...

If i change the 'XXX' declaration to not-a-constant, the value is

retrieved
successfully.



Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: David W. Fenton | last post by:
Today I was working on a hideous old app that I created a long time ago that does a lot of showing/hiding/resizing of fields on one of the forms. I had used constants to store reference values for...
2
by: Dan | last post by:
Let's say I have a class like: class Dummy { public const string CONE = "one"; public const string CTWO = "two"; ... other stuff .... }
8
by: farseer | last post by:
Hi, I have a class which defines a few integer Constants: public class AppConst { public const int XVALUE = 100; public const int YVALUE = 200; public const int YVALUE = 300; } I'd like to...
34
by: newsposter0123 | last post by:
The code block below initialized a r/w variable (usually .bss) to the value of pi. One, of many, problem is any linked compilation unit may change the global variable. Adjusting // rodata const...
17
by: Neil Cerutti | last post by:
The Glk API (which I'm implementing in native Python code) defines 120 or so constants that users must use. The constants already have fairly long names, e.g., gestalt_Version, evtype_Timer,...
2
by: wizofaus | last post by:
Given the following code: public class Test { static unsafe void StringManip(string data) { fixed (char* ps = data) ps = '$'; }
2
by: Leslie Sanford | last post by:
I want to define a set of floating point constants using templates insteand of macros. I'd like to determine whether these constants are floats or doubles at compile time. In my header file, I have...
54
by: shuisheng | last post by:
Dear All, I am always confused in using constants in multiple files. For global constants, I got some clues from http://msdn.microsoft.com/en-us/library/0d45ty2d(VS.80).aspx So in header...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.