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.

Re: GetType, Maybe by Phill

Phill
Thu Mar 13 08:01:41 CDT 2008

Brian wrote:

> 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.

Re: GetType, Maybe by Brian

Brian
Thu Mar 13 08:20:20 CDT 2008

On Mar 13, 9:01=A0am, "Phill W." <p-.-a-.-w-a-r...@-o-p-e-n-.-a-c-.-u-k>
wrote:
> Brian wrote:
> > 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,
> =A0 =A0 Phill =A0W.

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 =3D "abc"

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

Was hoping for something a little more elegant.

Thanks