How does one get the name of a variable which is passed to a method?
-
I wish to do something along the lines of: MyObject theObject = new MyObject(); DumpObject(theObject); ... elsewhere... public void DumpObject(Object obj) { string name = GetName(obj); // <<--- This is the part I'm missing System.Console.WriteLine("The name of the object is " + name); // this will print out: // "The name of the object is theObject" } I thought I could use reflection to get the name of the object but I'm not having any luck. I know this must be possible, but I'm stumped. TIA, Jeff
-
I wish to do something along the lines of: MyObject theObject = new MyObject(); DumpObject(theObject); ... elsewhere... public void DumpObject(Object obj) { string name = GetName(obj); // <<--- This is the part I'm missing System.Console.WriteLine("The name of the object is " + name); // this will print out: // "The name of the object is theObject" } I thought I could use reflection to get the name of the object but I'm not having any luck. I know this must be possible, but I'm stumped. TIA, Jeff
how you can get object name if it doesn't have Name property :confused:, or you just want to know its type?
dhaim program is hobby that make some money as side effect :)
-
I wish to do something along the lines of: MyObject theObject = new MyObject(); DumpObject(theObject); ... elsewhere... public void DumpObject(Object obj) { string name = GetName(obj); // <<--- This is the part I'm missing System.Console.WriteLine("The name of the object is " + name); // this will print out: // "The name of the object is theObject" } I thought I could use reflection to get the name of the object but I'm not having any luck. I know this must be possible, but I'm stumped. TIA, Jeff
Hi Jeff, 1. you are confusing objects with references. An object can have a name (say you provide a Name property), a reference only refers to the object. Also there could be many references to the same object. How about
MyObject theObject=new MyObject("Paul");
MyObject anotherReference=theObject;DumpObject(anotherReference);
Now which name would you want to get? The only one you will be able to get is "Paul" provided it got stored somewhere *inside* the object. 2. It is however possible to get the name of the reference itself, using reflection. But then you have to pass the reference itself, hence something like:
MyObject theObject=new MyObject("Paul");
MyObject anotherReference=theObject;DumpObject(ref anotherReference);
Now DumpObject gets hold of anotherReference itself, and through reflection should be able to get at its name. Haven't done this though, when names are important I store them inside the object. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Voting for dummies? No thanks. X|
-
how you can get object name if it doesn't have Name property :confused:, or you just want to know its type?
dhaim program is hobby that make some money as side effect :)
No, I can't assume that the object has a Name property, and I don't want to know the type (which I'd just do Type t = myObject.GetType()). I'm developing a (generic) debugging routine, so if I passed a variable named someObject: MyMethod(someObject); ...at runtime I want to get "someObject". I'd like to, and I'm sure there is a way, but I'm not seeing how to do it. Thanks though, Jeff
-
I wish to do something along the lines of: MyObject theObject = new MyObject(); DumpObject(theObject); ... elsewhere... public void DumpObject(Object obj) { string name = GetName(obj); // <<--- This is the part I'm missing System.Console.WriteLine("The name of the object is " + name); // this will print out: // "The name of the object is theObject" } I thought I could use reflection to get the name of the object but I'm not having any luck. I know this must be possible, but I'm stumped. TIA, Jeff
No, it's not possible. An instance of an object doesn't have a name, neither has the reference to it. The variable holding the reference does have a name, but that is not passed along to the method, neither is a reference to the variable. Also, unless the assembly is started with debug information, I don't think that you can even get the name of a local variable. There might not even be a variable holding the reference. Consider these examples: Here you have no variable at all:
DumpObject(new MyObject());
Here the variable is just a member of a KeyValuePair structure in a dictionary:
Dictionary<int,MyObject> MyDictionary = new Dictionary<int,MyObject>();
MyDictionary.Add(42, new MyObject());
DumpObject(MyDictionary[42]);What would you imagine that the name should be; "Value", "42" or "MyDictionary"?
Despite everything, the person most likely to be fooling you next is yourself.
-
Hi Jeff, 1. you are confusing objects with references. An object can have a name (say you provide a Name property), a reference only refers to the object. Also there could be many references to the same object. How about
MyObject theObject=new MyObject("Paul");
MyObject anotherReference=theObject;DumpObject(anotherReference);
Now which name would you want to get? The only one you will be able to get is "Paul" provided it got stored somewhere *inside* the object. 2. It is however possible to get the name of the reference itself, using reflection. But then you have to pass the reference itself, hence something like:
MyObject theObject=new MyObject("Paul");
MyObject anotherReference=theObject;DumpObject(ref anotherReference);
Now DumpObject gets hold of anotherReference itself, and through reflection should be able to get at its name. Haven't done this though, when names are important I store them inside the object. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Voting for dummies? No thanks. X|
Luc Pattyn wrote:
MyObject theObject=new MyObject("Paul"); MyObject anotherReference=theObject; DumpObject(anotherReference); Now which name would you want to get? The only one you will be able to get is "Paul" provided it got stored somewhere *inside* the object.
With the above code, I'd want to get the string "anotherReference" inside of DumpObject(). If the code was DumpObject(theObject), I'd want to get the string "theObject" inside of DumpObject().
Luc Pattyn wrote:
It is however possible to get the name of the reference itself, using reflection.
Luc Pattyn wrote:
and through reflection should be able to get at its name.
That's the part I'm not able to do. Thanks though, Jeff
-
No, it's not possible. An instance of an object doesn't have a name, neither has the reference to it. The variable holding the reference does have a name, but that is not passed along to the method, neither is a reference to the variable. Also, unless the assembly is started with debug information, I don't think that you can even get the name of a local variable. There might not even be a variable holding the reference. Consider these examples: Here you have no variable at all:
DumpObject(new MyObject());
Here the variable is just a member of a KeyValuePair structure in a dictionary:
Dictionary<int,MyObject> MyDictionary = new Dictionary<int,MyObject>();
MyDictionary.Add(42, new MyObject());
DumpObject(MyDictionary[42]);What would you imagine that the name should be; "Value", "42" or "MyDictionary"?
Despite everything, the person most likely to be fooling you next is yourself.
Right. What was I thinking again? so only solution is to put a Name inside the object, and forget reference names altogether. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Voting for dummies? No thanks. X|
-
Right. What was I thinking again? so only solution is to put a Name inside the object, and forget reference names altogether. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Voting for dummies? No thanks. X|
But then, what's the name of the Name? :-D
-
But then, what's the name of the Name? :-D
.NET 4.0 is rumored to provide introspection for that very purpose. :laugh:
Luc Pattyn [Forum Guidelines] [My Articles]
Voting for dummies? No thanks. X|
-
.NET 4.0 is rumored to provide introspection for that very purpose. :laugh:
Luc Pattyn [Forum Guidelines] [My Articles]
Voting for dummies? No thanks. X|
-
That's cool, but I can't wait that long. For now what I'm going to do is something like: DumpObject(theObject, "theObject"); Thanks, Jeff
OK. FYI: There used to be a little preprocessor trick to automate that in C and C++ (using some ## magic), but that is not available in C#... :)
Luc Pattyn [Forum Guidelines] [My Articles]
Voting for dummies? No thanks. X|
-
I wish to do something along the lines of: MyObject theObject = new MyObject(); DumpObject(theObject); ... elsewhere... public void DumpObject(Object obj) { string name = GetName(obj); // <<--- This is the part I'm missing System.Console.WriteLine("The name of the object is " + name); // this will print out: // "The name of the object is theObject" } I thought I could use reflection to get the name of the object but I'm not having any luck. I know this must be possible, but I'm stumped. TIA, Jeff
Hi, do you want to acess a object from a form in otherform?