Re: Get name of instance of object




"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


.



Relevant Pages

  • Re: GC performance - GC fragility
    ... I take care ). ... memory in an endless loop doesn't eat all the systems memory. ... reference an object that has been collected in the same way that you can ... otherwise allocation a single object would result in 100 ...
    (borland.public.delphi.non-technical)
  • RE: How to Reference a Property in a Linked Library - I think
    ... Just getting the XY Scatter chart to display properly was a near ... I have only used the Object Browser for the standard reference ... and do not know how to reference additional libraries ...
    (microsoft.public.access.modulesdaovba)
  • Re: referencing columns from list box?
    ... without the reference to Column) ... Doug Steele, Microsoft Access MVP ... Is there a way to get Me.MyTextbox to display ... The control source is Forest. ...
    (microsoft.public.access.formscoding)
  • Re: Are you as polite and diplomatic as COBOL?...
    ... The "balloon" approach might be a good option for some, ... Just do a quick reference in the N/E help file on those ... > decimals or without decimals. ... > enter 1234 and I display 1.234 ...
    (comp.lang.cobol)
  • Re: Display values set in a range??
    ... your reference argument is invalid as you don't have a letter ... when I'm attempting to display something within a particular ... cell from another cell, I end up using the INDIRECT, and ADDRESS functions ... column starting with the row that the MATCH function returned as a valid row ...
    (microsoft.public.excel.worksheet.functions)