class variable name
-
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.
-
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.
-
Ronald Bastien wrote: CallingSomething(MailingAddress) cant you just put "MailingAddress", including the quotes, as the paramater to pass to the CallingSomething procedure? ------------------------ Jordan. III
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.
-
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.
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
-
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
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.
-
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.
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.
-
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.
-
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.
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