Re: Get name of instance of object
- From: "Jon Slaughter" <Jon_Slaughter@xxxxxxxxxxx>
- Date: Sun, 8 Oct 2006 15:15:57 -0500
"Jon Skeet [C# MVP]" <skeet@xxxxxxxxx> wrote in message
news:MPG.1f9369e796e49b2a98d529@xxxxxxxxxxxxxxxxxxxxxxx
Jon Slaughter <Jon_Slaughter@xxxxxxxxxxx> wrote:
Is it possible to get the name of an instance of an object?
There's no such concept, I'm afraid.
I want to display it for debugging purposes but don't have a clue about
how
to go about it.
i.e., I want something like(psuedo)
Class A { }
main
{
A B;
writeline("{0}", B.Name());
}
and it display B.
I know I could just simply display B but if I end up changing the
instance
name then I would necessarily know where to update it then.
The thing is, that's not the name of the object - it's the name of *a*
variable which has a reference to the object. There may be multiple
variables referring to the same object, or there may be none.
If you want to give an object a name, you'll have to add that as a
property of the class.
I don't want the name of the object. I can get that with GetType or
ToString? I want the name of the instance(or variable as you said).
So if I have 3 instances or references then I only care about there name and
not where they point.
so in the above if I have A B1, B2, B3;
then I really only want to get B1, B2, B3. I understand that there might be
some problems here. But when I do B1.Name() for example, I would want it to
return "B1". I don't mind the if the compiler just looked to the left of
the . and took that token and then replace the statement with a string of
it.
i.e., X.Name() should return "X" would be nice.
What this means is that I can refactor completely and it will take care of
itself. if I did something like WriteLine("X"); and I later renamed my
variable to Y then I will still be printing X but if I had something like
above I could just refactor and not worry about it.
Does that make sense?
Thanks,
Jon
.
- Follow-Ups:
- Re: Get name of instance of object
- From: Jon Skeet [C# MVP]
- Re: Get name of instance of object
- References:
- Get name of instance of object
- From: Jon Slaughter
- Re: Get name of instance of object
- From: Jon Skeet [C# MVP]
- Get name of instance of object
- Prev by Date: Re: Get name of instance of object
- Next by Date: Re: get / set and sevral values
- Previous by thread: Re: Get name of instance of object
- Next by thread: Re: Get name of instance of object
- Index(es):
Relevant Pages
|