Store Variable Name in String in VB.NET

14,320

Solution 1

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.

Solution 2

Does this example from msdn using reflection help?

Solution 3

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"

Solution 4

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.

Share:
14,320
Nick Gotch
Author by

Nick Gotch

I've been a software engineer in a variety of industries over the past 15 years. The majority of that time has been developing business software for academic, financial, healthcare, and management services, primarily in C#.NET (starting with .NET 1.1) and Python, but also with a fair amount of Perl and a mix of other languages (C++, VB.NET, Lua, etc.). I've also spent about 5 years of that time in the mobile games industry, working on the Android release of Fieldrunners and the server backend for Fieldrunners Attack!.

Updated on June 08, 2022

Comments

  • Nick Gotch
    Nick Gotch almost 2 years

    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
    Nick Gotch over 15 years
    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.
  • Jon Skeet
    Jon Skeet over 15 years
    But when you say "the variable would be known" - in what way? What would know it, and in what form?
  • Nick Gotch
    Nick Gotch over 15 years
    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!