views:

3613

answers:

5

I'm trying to store the names of some variables inside strings. For example:

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 would help with some debugging I'm working on.

+4  A: 

Well, you can clearly just set Foo1Name = "Foo1" - but I strongly suspect that's not what you're after.

How would you know which variable you're trying to find the name of? What's the bigger picture? What you want may be possible with reflection, if we're talking about non-local variables, but I suspect it's either not feasible, or there's a better way to attack the problem in the first place.

Jon Skeet
The variable would be known based on where the exception arises. I have a form composed of many controls and want to store the name of the variabls in a DB table (automating load testing.) Reflection or something like System.Enum.GetNames(GetType(EnumName)) but with variables is what I was thinking.
Nick Gotch
But when you say "the variable would be known" - in what way? What would know it, and in what form?
Jon Skeet
I have a method called LogToDB(control, controlName) which is redundant (all my calls are like LogToDB(txtFoo, "txtFoo") .) I wanted to avoid repeating the name in these, but I guess any way around this would be just as redundant.Thanks for the help!
Nick Gotch
+1  A: 

Does this example from msdn using reflection help?

Rob

RobS
Doesn't solve my specific issue but it is helpful, thanks.
Nick Gotch
A: 

One solution would be to use an associative array to store your variables. Once, I did this in .Net, but I think I wrote a custom class to do it.

myArray("foo1Name") = "foo1"

Then, you can just store a list of your variable names, or you can wrap that up in the same class.

if( myArray(variableName(x)) == whatImLookingFor ) print variableName(x) & "is it"
PaulMorel
A: 

For finding the variable name, see: http://stackoverflow.com/questions/72121/finding-the-variable-name-passed-to-a-function-in-c

This would apply to VB.Net as well.

Robert Claypool
+1  A: 

I think this really depends on what you are trying to debug. Two possible things to look at are the Reflection and StackTrace classes. That said when your program is compiled, the compiler and runtime do not guarantee that that names need to be consistent with the original program.

This is especially the case with debug vs. release builds. The point of the .PDB files (symbols) in the debug version are to include more information about the original program. For native C/C++ applications it is strongly recommended that you generate symbols for every build (debug+release) of your application to help with debugging. In .NET this is less of an issue since there are features like Reflection. IIRC John Robbins recommends that you always generate symbols for .NET projects too.

You might also find Mike Stall's blog useful and the managed debugger samples.

BrianLy