GetType, Maybe

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian

    GetType, Maybe

    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.ToStri ng)

    Debug.Print(int 123)
    Debug.Print(int 123.GetType.ToS tring)
    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.
  • Phill W.

    #2
    Re: GetType, Maybe

    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.

    Comment

    • Brian

      #3
      Re: GetType, Maybe

      On Mar 13, 9:01 am, "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,
          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

      Comment

      Working...