Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. class variable name

class variable name

Scheduled Pinned Locked Moved Visual Basic
questioncsharp
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Ronald Bastien
    wrote on last edited by
    #1

    Hi all, Is there a way to retrieve the name of the variable containing an object passed to a subrouting. Here is a simple illustration meant to explain my question; I have the following two class defintions Public Class Address public City as string ... End Class Public Class Organisation Public MailingAddress as new Address public BillingAddress as new Address public sub CallSomething() ' pass the object whose variable name we want to display CallingSomething(MailingAddress) end sub public sub CallingSomething(objAddress as Address) 'display the name of the variable of objAddress as it is known in my Class Organisation. The display should be "MailingAddress". end sub End Class Using objAddress.GetType.Name gives me the "Address" which is the class name but I'm looking for the variable name "MailingAddress". If I had passed the BillingAddress, obviously I would want to display "BillingAddress". Can this be done in VB.NET? Thanks in advance. Ronald Bastien. Strategium Technologies inc.

    N J B 3 Replies Last reply
    0
    • R Ronald Bastien

      Hi all, Is there a way to retrieve the name of the variable containing an object passed to a subrouting. Here is a simple illustration meant to explain my question; I have the following two class defintions Public Class Address public City as string ... End Class Public Class Organisation Public MailingAddress as new Address public BillingAddress as new Address public sub CallSomething() ' pass the object whose variable name we want to display CallingSomething(MailingAddress) end sub public sub CallingSomething(objAddress as Address) 'display the name of the variable of objAddress as it is known in my Class Organisation. The display should be "MailingAddress". end sub End Class Using objAddress.GetType.Name gives me the "Address" which is the class name but I'm looking for the variable name "MailingAddress". If I had passed the BillingAddress, obviously I would want to display "BillingAddress". Can this be done in VB.NET? Thanks in advance. Ronald Bastien. Strategium Technologies inc.

      N Offline
      N Offline
      Nadroj
      wrote on last edited by
      #2

      Ronald Bastien wrote: CallingSomething(MailingAddress) cant you just put "MailingAddress", including the quotes, as the paramater to pass to the CallingSomething procedure? ------------------------ Jordan. III

      R 1 Reply Last reply
      0
      • N Nadroj

        Ronald Bastien wrote: CallingSomething(MailingAddress) cant you just put "MailingAddress", including the quotes, as the paramater to pass to the CallingSomething procedure? ------------------------ Jordan. III

        R Offline
        R Offline
        Ronald Bastien
        wrote on last edited by
        #3

        I could but I'm already passing the object so I don't want to pass the name as well. This object can also be passed down through an hierachy of calls and again, I don't want to have to pass the name with the object. Cheers. Ronald Bastien Strategium Technologies inc.

        N 1 Reply Last reply
        0
        • R Ronald Bastien

          I could but I'm already passing the object so I don't want to pass the name as well. This object can also be passed down through an hierachy of calls and again, I don't want to have to pass the name with the object. Cheers. Ronald Bastien Strategium Technologies inc.

          N Offline
          N Offline
          Nadroj
          wrote on last edited by
          #4

          i must not be understanding, or too new at this. " public sub CallingSomething(objAddress as Address) 'display the name of the variable of objAddress as it is known in my Class Organisation. The display should be "MailingAddress". end sub " if you send the string "MailingAddress" to this procedure, from the CallSomething procedure, it will do what it says you want here: "The display should be "MailingAddress." sorry im :confused: what are you going to do with the variable name once it is passed? just display it? ------------------------ Jordan. III

          R 1 Reply Last reply
          0
          • N Nadroj

            i must not be understanding, or too new at this. " public sub CallingSomething(objAddress as Address) 'display the name of the variable of objAddress as it is known in my Class Organisation. The display should be "MailingAddress". end sub " if you send the string "MailingAddress" to this procedure, from the CallSomething procedure, it will do what it says you want here: "The display should be "MailingAddress." sorry im :confused: what are you going to do with the variable name once it is passed? just display it? ------------------------ Jordan. III

            R Offline
            R Offline
            Ronald Bastien
            wrote on last edited by
            #5

            Jordan, You're right but I'm oversimplyfying my code to give an example of what I'm looking for. Is it possible in the CallingSomething routine to access the objAddress object and through some property (from the framework) get the name the variable from the caller? The thing is, I need to pass the object so I don't want to pass an additional string with it's name. I'm also passing this object to other routines so this makes it a less attractive solution. Cheers Ronald Bastien Strategium Technologies inc.

            N 1 Reply Last reply
            0
            • R Ronald Bastien

              Hi all, Is there a way to retrieve the name of the variable containing an object passed to a subrouting. Here is a simple illustration meant to explain my question; I have the following two class defintions Public Class Address public City as string ... End Class Public Class Organisation Public MailingAddress as new Address public BillingAddress as new Address public sub CallSomething() ' pass the object whose variable name we want to display CallingSomething(MailingAddress) end sub public sub CallingSomething(objAddress as Address) 'display the name of the variable of objAddress as it is known in my Class Organisation. The display should be "MailingAddress". end sub End Class Using objAddress.GetType.Name gives me the "Address" which is the class name but I'm looking for the variable name "MailingAddress". If I had passed the BillingAddress, obviously I would want to display "BillingAddress". Can this be done in VB.NET? Thanks in advance. Ronald Bastien. Strategium Technologies inc.

              J Offline
              J Offline
              John Kuhn
              wrote on last edited by
              #6

              Unless I'm mistaken (which could certainly be the case) the ability that you're looking for is contained in the System.Reflection namespace. What a piece of work is man, how noble in reason, how infinite in faculties, in form and moving how express and admirable . . . and yet to me, what is this quintessence of dust? -- Hamlet, Act II, Scene ii.

              1 Reply Last reply
              0
              • R Ronald Bastien

                Jordan, You're right but I'm oversimplyfying my code to give an example of what I'm looking for. Is it possible in the CallingSomething routine to access the objAddress object and through some property (from the framework) get the name the variable from the caller? The thing is, I need to pass the object so I don't want to pass an additional string with it's name. I'm also passing this object to other routines so this makes it a less attractive solution. Cheers Ronald Bastien Strategium Technologies inc.

                N Offline
                N Offline
                Nadroj
                wrote on last edited by
                #7

                hmmm... sorry iv been looking into it, and fooling around, and of course cant figure it out. i hope someone can help. please let me know the solution, if one arises, Ron. gNite.:zzz: ------------------------ Jordan. III

                1 Reply Last reply
                0
                • R Ronald Bastien

                  Hi all, Is there a way to retrieve the name of the variable containing an object passed to a subrouting. Here is a simple illustration meant to explain my question; I have the following two class defintions Public Class Address public City as string ... End Class Public Class Organisation Public MailingAddress as new Address public BillingAddress as new Address public sub CallSomething() ' pass the object whose variable name we want to display CallingSomething(MailingAddress) end sub public sub CallingSomething(objAddress as Address) 'display the name of the variable of objAddress as it is known in my Class Organisation. The display should be "MailingAddress". end sub End Class Using objAddress.GetType.Name gives me the "Address" which is the class name but I'm looking for the variable name "MailingAddress". If I had passed the BillingAddress, obviously I would want to display "BillingAddress". Can this be done in VB.NET? Thanks in advance. Ronald Bastien. Strategium Technologies inc.

                  B Offline
                  B Offline
                  boogs
                  wrote on last edited by
                  #8

                  There is no way to do this in C#. System.Diagnostics.StackFrame gets the closest, but it does not know about instance information; only types. There is a probably a more natural way to get what you want. Maybe passing in an object which has Address members and letting CallingSomething iterate it's members looking for them? Then it could output all the members which have a certain type by name. Without knowing more about the requirements, it's hard to say. -- Aaron

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups