Return Const Name from value?

  • Thread starter Thread starter William Ryan
  • Start date Start date
W

William Ryan

If I understand what you want to do,...

Const varA = 200.

So in code you want to pass in 200 and get back varA? If so, I'd build a
hashtable with all of them and use the numeric as the index value.
 
This may sound like a stupid stupid question and I figure it would b
more "general" than pertaining to a specific Language.

I'm using vb.net and I have a bunch of Const values in my program.
can use them obviously by placing their names in place of values, henc
the ideal behind using Const.

My question is... is there a way to get the actual Variable name for
specific value that is returned to me?

Say for instance I have:

Const varA = 20
Const varB = 30
...

Somewhere in my program, I get a value returned to me from a function
(i.e. this is a keyboard hook I'm implementing). The result gives m
an integer value, for where I'd like to spit out the NAME of th
variable that would be associated with it.

Mind you I feel this would cause too much overhead for the programmin
language to constantly compare returned values to see if they are equa
to any const values etc.

Is there anyway to do this at all? I want to stay away from say like
major case statement because I have almost 200 Const values, fo
certain Hex numbers representing each key that is pressed.

Please help if you can

mcdonam
 
mcdonamw,
In addition to William's comments.

Instead of defining Const, consider defining an Enum.

Then you can use System.Enum.GetName to convert the Enum value to the Enum
Name.

Hope this helps
Jay
 
Jay:

In light of your response, let me retract my original one. I was thinking
in HashTables all day, but I'm with you, an Enum would make life a whole lot
nicer!
 
Hi McDonaw,

In addition to Jay B.

Did you look at the already build in enum for keys?
(Maybe you only needs additions to that, but I would keep it to the same
schema)
Is there anyway to do this at all? I want to stay away from say like a
major case statement because I have almost 200 Const values, for
certain Hex numbers representing each key that is pressed.

I hope this helps?

Cor
 
Here's a also a way to match a literal value to an Enum member and see if it
exists there.

private object EnumValueFromLiteral(System.Type t, string name)
{
MemberInfo[] mi = t.FindMembers(MemberTypes.Field,
BindingFlags.Static |
BindingFlags.Public,
Type.FilterNameIgnoreCase, name);

// Can't find the thing in the enum.
if (mi.Length == 0)
throw new Exception(string.Format("Element '{0}' not found in enum
'{1}'", name, t.Name));

FieldInfo f = mi[0] as FieldInfo;
return f.GetValue(null);

}

Weird =)
 
Klaus,
How do you use your code?

It appears that you implemented the System.Enum.Parse method using
reflection.

http://msdn.microsoft.com/library/d...cpref/html/frlrfSystemEnumClassParseTopic.asp

Is there an advantage to your code over using System.Enum.Parse or the other
static/shared members of System.Enum?

Don't get me wrong, reflection is very powerful, and I like learning new
ways to use it. I'm just not seeing what specifically you are doing, or how
using reflection is "Better" then using one of the static/shared members
already available to us on System.Enum.

Thanks for any further insite!

Jay

Klaus H. Probst said:
Here's a also a way to match a literal value to an Enum member and see if it
exists there.

private object EnumValueFromLiteral(System.Type t, string name)
{
MemberInfo[] mi = t.FindMembers(MemberTypes.Field,
BindingFlags.Static |
BindingFlags.Public,
Type.FilterNameIgnoreCase, name);

// Can't find the thing in the enum.
if (mi.Length == 0)
throw new Exception(string.Format("Element '{0}' not found in enum
'{1}'", name, t.Name));

FieldInfo f = mi[0] as FieldInfo;
return f.GetValue(null);

}

Weird =)


--
____________________
Klaus H. Probst, MVP
http://www.vbbox.com/


mcdonamw said:
This may sound like a stupid stupid question and I figure it would be
more "general" than pertaining to a specific Language.

I'm using vb.net and I have a bunch of Const values in my program. I
can use them obviously by placing their names in place of values, hence
the ideal behind using Const.

My question is... is there a way to get the actual Variable name for a
specific value that is returned to me?

Say for instance I have:

Const varA = 20
Const varB = 30
..

Somewhere in my program, I get a value returned to me from a function.
(i.e. this is a keyboard hook I'm implementing). The result gives me
an integer value, for where I'd like to spit out the NAME of the
variable that would be associated with it.

Mind you I feel this would cause too much overhead for the programming
language to constantly compare returned values to see if they are equal
to any const values etc.

Is there anyway to do this at all? I want to stay away from say like a
major case statement because I have almost 200 Const values, for
certain Hex numbers representing each key that is pressed.

Please help if you can!


mcdonamw
 
Hi Jay,

Well, it's kind of freaky =)

Of course you can do this with one line:

int theValue = (int) System.Enum.Parse(typeof(MyEnum), "Name",true);
But hey <g>


--
klaus



Jay B. Harlow said:
Klaus,
How do you use your code?

It appears that you implemented the System.Enum.Parse method using
reflection.

http://msdn.microsoft.com/library/d...cpref/html/frlrfSystemEnumClassParseTopic.asp

Is there an advantage to your code over using System.Enum.Parse or the other
static/shared members of System.Enum?

Don't get me wrong, reflection is very powerful, and I like learning new
ways to use it. I'm just not seeing what specifically you are doing, or how
using reflection is "Better" then using one of the static/shared members
already available to us on System.Enum.

Thanks for any further insite!

Jay

Klaus H. Probst said:
Here's a also a way to match a literal value to an Enum member and see
if
it
exists there.

private object EnumValueFromLiteral(System.Type t, string name)
{
MemberInfo[] mi = t.FindMembers(MemberTypes.Field,
BindingFlags.Static |
BindingFlags.Public,
Type.FilterNameIgnoreCase, name);

// Can't find the thing in the enum.
if (mi.Length == 0)
throw new Exception(string.Format("Element '{0}' not found in enum
'{1}'", name, t.Name));

FieldInfo f = mi[0] as FieldInfo;
return f.GetValue(null);

}

Weird =)


--
____________________
Klaus H. Probst, MVP
http://www.vbbox.com/


mcdonamw said:
This may sound like a stupid stupid question and I figure it would be
more "general" than pertaining to a specific Language.

I'm using vb.net and I have a bunch of Const values in my program. I
can use them obviously by placing their names in place of values, hence
the ideal behind using Const.

My question is... is there a way to get the actual Variable name for a
specific value that is returned to me?

Say for instance I have:

Const varA = 20
Const varB = 30
..

Somewhere in my program, I get a value returned to me from a function.
(i.e. this is a keyboard hook I'm implementing). The result gives me
an integer value, for where I'd like to spit out the NAME of the
variable that would be associated with it.

Mind you I feel this would cause too much overhead for the programming
language to constantly compare returned values to see if they are equal
to any const values etc.

Is there anyway to do this at all? I want to stay away from say like a
major case statement because I have almost 200 Const values, for
certain Hex numbers representing each key that is pressed.

Please help if you can!


mcdonamw
------------------------------------------------------------------------------------------------------------------------------------------------
 
Back
Top