GetType, Maybe

  • Thread starter Thread starter Brian
  • Start date Start date
B

Brian

The following code produces the following results.
Sub Main()
Dim abc As String = "This is a string"
Dim int123 As Integer = 123

Debug.Print(abc)
Debug.Print(abc.GetType.ToString)

Debug.Print(int123)
Debug.Print(int123.GetType.ToString)
End Sub

This is a string
System.String
123
System.Int32

Is there a function that would return the variable name?

Such as abc and int123.

Kind regards.
 
Brian said:
Is there a function that would return the variable name?

Not really, no.
By the time your code [has been through the compiler, been J.I.T.
link-loaded and finally] runs, the variable names, even if they still
existed, would be utterly meaningless.

Why do you feel the /need/ for this, though?

IME, this question is often asked by people trying to get hold the
/Name/ of a Control so that they can use this name later on to "get back
to" the Control (it's far better to get a reference to the Control and
use that instead).

HTH,
Phill W.
 
Brian said:
Is there a function that would return the variable name?

Not really, no.
By the time your code [has been through the compiler, been J.I.T.
link-loaded and finally] runs, the variable names, even if they still
existed, would be utterly meaningless.

Why do you feel the /need/ for this, though?

IME, this question is often asked by people trying to get hold the
/Name/ of a Control so that they can use this name later on to "get back
to" the Control (it's far better to get a reference to the Control and
use that instead).

HTH,
    Phill  W.

No real reason, just poking around under the hood of VB in the IDE. I
found that I could easily get the type information and thought there
must be a way to get the name.

I was just testing some code for writing to a text file, I am
capturing the Exception information in a Try Catch.
Thought it would be nice to also capture the value in each of the
variables while I was at it. I didn't want to have to write a bunch of
code to capture like this
Dim ABC As String = "abc"

in a Catch....
Debug.print "ABC:" & ABC

Was hoping for something a little more elegant.

Thanks
 
Back
Top