3

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.

Nick Gotch
  • 9,167
  • 14
  • 70
  • 97
  • i hate being that guy, but this is really not the way to do whatever you aretrying to do – Shawn Dec 09 '08 at 18:09

5 Answers5

5

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
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 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 Dec 09 '08 at 15:47
  • But when you say "the variable would be known" - in what way? What would know it, and in what form? – Jon Skeet Dec 09 '08 at 15:49
  • 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 Dec 09 '08 at 16:14
2

Does this example from msdn using reflection help?

Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
RobS
  • 3,807
  • 27
  • 34
1

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"
Paul Morel
  • 551
  • 2
  • 13
1

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.

Brian Lyttle
  • 14,558
  • 15
  • 68
  • 104
0

For finding the variable name, see: Finding the variable name passed to a function

This would apply to VB.Net as well.

Community
  • 1
  • 1
Robert Claypool
  • 4,262
  • 9
  • 50
  • 61