473,320 Members | 1,848 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,320 software developers and data experts.

Return Const Name from 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
-----------------------------------------------------------------------
Posted via http://www.mcse.m
-----------------------------------------------------------------------
View this thread: http://www.mcse.ms/message229047.htm

Jul 21 '05 #1
7 1908
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.
"mcdonamw" <mc************@mail.mcse.ms> wrote in message
news:mc************@mail.mcse.ms...

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
------------------------------------------------------------------------
Posted via http://www.mcse.ms
------------------------------------------------------------------------
View this thread: http://www.mcse.ms/message229047.html

Jul 21 '05 #2
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

"mcdonamw" <mc************@mail.mcse.ms> wrote in message
news:mc************@mail.mcse.ms...

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
------------------------------------------------------------------------
Posted via http://www.mcse.ms
------------------------------------------------------------------------
View this thread: http://www.mcse.ms/message229047.html

Jul 21 '05 #3
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!
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:eU**************@TK2MSFTNGP10.phx.gbl...
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

"mcdonamw" <mc************@mail.mcse.ms> wrote in message
news:mc************@mail.mcse.ms...

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
------------------------------------------------------------------------
Posted via http://www.mcse.ms
------------------------------------------------------------------------
View this thread: http://www.mcse.ms/message229047.html


Jul 21 '05 #4
Cor
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
Jul 21 '05 #5
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" <mc************@mail.mcse.ms> wrote in message
news:mc************@mail.mcse.ms...

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
------------------------------------------------------------------------
Posted via http://www.mcse.ms
------------------------------------------------------------------------
View this thread: http://www.mcse.ms/message229047.html

Jul 21 '05 #6
Klaus,
How do you use your code?

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

http://msdn.microsoft.com/library/de...ParseTopic.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" <us*******@vbbox.com> wrote in message
news:eg**************@TK2MSFTNGP10.phx.gbl...
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" <mc************@mail.mcse.ms> wrote in message
news:mc************@mail.mcse.ms...

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
------------------------------------------------------------------------
Posted via http://www.mcse.ms
------------------------------------------------------------------------
View this thread: http://www.mcse.ms/message229047.html


Jul 21 '05 #7
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 [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:ug**************@TK2MSFTNGP09.phx.gbl...
Klaus,
How do you use your code?

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

http://msdn.microsoft.com/library/de...ParseTopic.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" <us*******@vbbox.com> wrote in message
news:eg**************@TK2MSFTNGP10.phx.gbl...
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" <mc************@mail.mcse.ms> wrote in message
news:mc************@mail.mcse.ms...

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

------------------------------------------------------------------------ Posted via http://www.mcse.ms

------------------------------------------------------------------------
View this thread: http://www.mcse.ms/message229047.html



Jul 21 '05 #8

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

Similar topics

19
by: Christian Engström | last post by:
If you have a function that returns something by value, the gcc compiler (version 3.2.3 on Windows XP with MinGW) converts the returned value from the type you specify in the code, to the const...
5
by: Bob | last post by:
Hi, I have a std::vector, say myVec, of some user defined object, say myOb. In my code, I have a function that searches myVec for a particular myOb. The way I was doing this was searching...
5
by: Neal Coombes | last post by:
Posted to comp.lang.c++.moderated with little response. Hoping for better from the unmoderated groups: -------- Original Message -------- Subject: Return appropriately by value, (smart)...
7
by: mcdonamw | last post by:
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...
2
by: Eric Lilja | last post by:
Hello, consider this complete program: #include <iostream> #include <string> using std::cout; using std::endl; using std::string; class Hanna {
5
by: Shea Martin | last post by:
I have a struct like so: struct MyStruct { public: void Value( int newValue ) { mValue = newValue; } int Value() const { return mValue; } private: int mValue;
32
by: Axel Bock | last post by:
Hi all, I am trying to get my head around what happens if I return a class object from a function. It seems C++ (MinGW) does not invoke the copy constructor if I do something like this: ...
12
by: Aff | last post by:
Brothers , i am facing a problem which is as follow: class poly { private: char name; int capacity; public: poly(char , int );
1
by: PengYu.UT | last post by:
Hi, Are there any walkaround to enable functions in the derived class with the same function name but different return type? In the following example, D1 and D2 are B's derived class. I want...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.