Save variable name to String in VB.NET

I am trying to keep the names of some variables inside strings. For instance:

Dim Foo1 as Integer Dim Foo1Name as String ' -- Do something to set Foo1Name to the name of the other variable -- MessageBox.Show(Foo1Name & " is the variable you are looking for.") ' Outputs: ' Foo1 is the variable you are looking for. 

This will help with some debugging that I'm working on.

+3
source share
5 answers

Ok, you can just set Foo1Name = "Foo1" - but I strongly suspect that not what you need.

How do you know which variable you are trying to find? What a big picture? What you want may be possible with reflection if we are talking about non-local variables, but I suspect that this is either impossible, or there is a better way to attack the problem in the first place.

+5
source

Is this an example from msdn using reflection help?

+2
source

One solution would be to use an associative array to store your variables. I once did this in .Net, but I think I wrote a special class for this.

 myArray("foo1Name") = "foo1" 

Then you can just save the list of variable names, or you can wrap it in the same class.

 if( myArray(variableName(x)) == whatImLookingFor ) print variableName(x) & "is it" 
+1
source

I think it really depends on what you are trying to debug. Two possibilities are possible: Reflection and StackTrace . However, when your program is compiled, the compiler and runtime do not guarantee that these names must correspond to the original program.

This is especially true for debug vs. assembly. release. The point .PDB files (characters) in the debug version should include additional information about the source program. For native C / C ++ applications, it is highly recommended that you create symbols for each assembly (debug + release) of your application to aid in debugging. In .NET, this is not a problem since there are features like Reflection. IIRC John Robbins recommends always generating characters for .NET projects.

You can also find the Mike Stall blog and a managed debugger .

+1
source

To find a variable name, see Searching for a Variable Name Passed to a Function

This also applies to VB.Net.

0
source

All Articles