Get name of instance of object

J

Jon Slaughter

Is it possible to get the name of an instance of an object?

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. I could
refactor/search and replace ofcourse but it seems better to put it in code.

Thanks,
Jon
 
J

Jon Slaughter

I also need to print out the addresses of variables... can't seem to do this
either as when I just put in the object name to print it doe sthe same as if
I used GetType()
 
J

Jon Skeet [C# MVP]

Jon Slaughter said:
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.
 
J

Jon Slaughter

Jon Skeet said:
There's no such concept, I'm afraid.


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
 
J

Jon Skeet [C# MVP]

Jon Slaughter said:
I don't want the name of the object. I can get that with GetType or
ToString?

Well, again neither of those give you the name, as there's no such
thing.
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?

Sort of, although I can't say I've ever wanted to do it myself - and C#
has no such facility.
 
A

Andrei Bazhgin

I just know this is how you would do it in C/C++:

#define debug_print(msg, object) printf("%s - %s", msg, ##object);

##object is translated to the string of what that object is in the
pre-preprocessed code.

- peace
 
J

Joanna Carter [TeamB]

"Jon Slaughter" <[email protected]> a écrit dans le message de (e-mail address removed)...

| Is it possible to get the name of an instance of an object?
|
| 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. I could
| refactor/search and replace ofcourse but it seems better to put it in
code.

The names of variables are not maintained by the compiler, they are only
there for humans to read in the source code. Try decompiling an assemby
using Reflector and you will find that the variable names are different from
those in the original source; things like string1, etc will appear.

VS has very good refactoring support; if you rename a variable, all you have
to do is press Ctrl-. and it will replace all occurrences of that variable
name with the new name.

Joanna
 
J

Joanna Carter [TeamB]

"Jon Slaughter" <[email protected]> a écrit dans le message de (e-mail address removed)...

| I also need to print out the addresses of variables... can't seem to do
this
| either as when I just put in the object name to print it doe sthe same as
if
| I used GetType()

..NEt objects are relocatable at runtime, by the GC, therefore addresses are
meaningless.

Joanna
 
J

Jon Slaughter

Joanna Carter said:
"Jon Slaughter" <[email protected]> a écrit dans le message de
(e-mail address removed)...

| Is it possible to get the name of an instance of an object?
|
| 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. I could
| refactor/search and replace ofcourse but it seems better to put it in
code.

The names of variables are not maintained by the compiler, they are only
there for humans to read in the source code. Try decompiling an assemby
using Reflector and you will find that the variable names are different
from
those in the original source; things like string1, etc will appear.

VS has very good refactoring support; if you rename a variable, all you
have
to do is press Ctrl-. and it will replace all occurrences of that variable
name with the new name.

and it does this for references to these instances inside a string? I don't
think so? The whole point is to make things make sense. I'm not trying to
get something at runtime or in an assembly but to turn a variables name into
text so I can display this at runtime. This is a similar idea to using
enums.

its very simple: I want to be able to display the an instances name and its
address for debugging purposes. Why? So I know exactly whats going on. It
would be nice also to display the line number where the message was printed.
These things are easy to do in other languages but I see no equivilent in
C#.
 
J

Jon Slaughter

Andrei Bazhgin said:
I just know this is how you would do it in C/C++:

#define debug_print(msg, object) printf("%s - %s", msg, ##object);

##object is translated to the string of what that object is in the
pre-preprocessed code.

Unfortunately I'm not using C/C++ and I don't think there are eqivilent
preprocessors in C#.
 
J

Jon Slaughter

Joanna Carter said:
"Jon Slaughter" <[email protected]> a écrit dans le message de
(e-mail address removed)...

| I also need to print out the addresses of variables... can't seem to do
this
| either as when I just put in the object name to print it doe sthe same
as
if
| I used GetType()

.NEt objects are relocatable at runtime, by the GC, therefore addresses
are
meaningless.

if thats the case then how do you explain the fact that a program can work
with memory? addresses are not meaningless but maybe you mean that they are
just inconsistent? (Although I would still like the ability to do this
since I know the GC will not be a problem in my example.)

I suppose I could write an unmanaged C++ function and somehow wrap it.
 
J

Jon Slaughter

Jon Skeet said:
Well, again neither of those give you the name, as there's no such
thing.


Sort of, although I can't say I've ever wanted to do it myself - and C#
has no such facility.

Well, I see the problem as something like

SomeClass myVar;

Writeln("myVar");

and when I refactor then obviously there will be no change in the string.

but if I'm able to do something like

Writeln(Name(myVar));
or
Writeln(myVar.Name());

Then the refactoring method will work because it see's myVar not as a string
but as an instance... even though myVar.Name() = "myVar" = Name(myVar) or
whatever.

Hence I won't have to do a search and replace if I want to change myVar.
Ofcoures thats essentially what refactoring will do but it won't work inside
the strings.
 
J

Jon Slaughter

Joanna Carter said:
"Jon Slaughter" <[email protected]> a écrit dans le message de
(e-mail address removed)...

| I also need to print out the addresses of variables... can't seem to do
this
| either as when I just put in the object name to print it doe sthe same
as
if
| I used GetType()

.NEt objects are relocatable at runtime, by the GC, therefore addresses
are
meaningless.

Also, what about the fixed statement?
 
J

Joanna Carter [TeamB]

"Jon Slaughter" <[email protected]> a écrit dans le message de (e-mail address removed)...

| and it does this for references to these instances inside a string? I
don't
| think so? The whole point is to make things make sense. I'm not trying to
| get something at runtime or in an assembly but to turn a variables name
into
| text so I can display this at runtime. This is a similar idea to using
| enums.

I suggest that you are trying to do something that is not normal in other
languages as well in C#. In over ten years, I certainly have never found a
need to do what you are suggesting. If you really want to continue with this
unusual quest, then I suggest you use straightforward search and replace in
your source files to achieve any possible refactoring.

| its very simple: I want to be able to display the an instances name and
its
| address for debugging purposes. Why? So I know exactly whats going on. It
| would be nice also to display the line number where the message was
printed.
| These things are easy to do in other languages but I see no equivilent in
| C#.

There are better ways of instumenting code, take a look at AOP and how .NET
can use ContextBoundObject.

OTOH, you could simply ensure that your code is bug-free as you write it,
maybe by using unit testing, where you write the tests first and then write
the code to pass the tests; much more sensible than searching for errors
once the code is written.

Joanna
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


That is no possible, and frankly I find it not needed at all.
 
J

Jon Slaughter

Joanna Carter said:
"Jon Slaughter" <[email protected]> a écrit dans le message de
(e-mail address removed)...

| and it does this for references to these instances inside a string? I
don't
| think so? The whole point is to make things make sense. I'm not trying
to
| get something at runtime or in an assembly but to turn a variables name
into
| text so I can display this at runtime. This is a similar idea to using
| enums.

I suggest that you are trying to do something that is not normal in other
languages as well in C#. In over ten years, I certainly have never found a
need to do what you are suggesting. If you really want to continue with
this
unusual quest, then I suggest you use straightforward search and replace
in
your source files to achieve any possible refactoring.

I guess since you've never needed it then it means no one should ever need
it?
| its very simple: I want to be able to display the an instances name and
its
| address for debugging purposes. Why? So I know exactly whats going on.
It
| would be nice also to display the line number where the message was
printed.
| These things are easy to do in other languages but I see no equivilent
in
| C#.

There are better ways of instumenting code, take a look at AOP and how
.NET
can use ContextBoundObject.

OTOH, you could simply ensure that your code is bug-free as you write it,
maybe by using unit testing, where you write the tests first and then
write
the code to pass the tests; much more sensible than searching for errors
once the code is written.

You could. But then again I am not perfect like you. I make many mistakes
and its nice to have some idea where they occur.
 
J

Jon Slaughter

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,


That is no possible, and frankly I find it not needed at all.

Man, it amazes me why so many people think that just because they don't need
something that it means no one else needs it either. (sounds like some type
of ego trip to me).

Oh, and why is it not possible? I can write a simple utility that "extends"
the C# syntax to include this ability. Surely with your vast programming
experience you can write the complex code to do a search and replace?
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Joanna said:
"Jon Slaughter" <[email protected]> a écrit dans le message de (e-mail address removed)...
| and it does this for references to these instances inside a string? I don't
| think so? The whole point is to make things make sense. I'm not trying to
| get something at runtime or in an assembly but to turn a variables name into
| text so I can display this at runtime. This is a similar idea to using
| enums.

I suggest that you are trying to do something that is not normal in other
languages as well in C#. In over ten years, I certainly have never found a
need to do what you are suggesting. If you really want to continue with this
unusual quest, then I suggest you use straightforward search and replace in
your source files to achieve any possible refactoring.

As someone has already mentioned, then it is possible via
the C/C++ preprocesser.

And it seen used occasionally. Not often but it happens.

It is not possible in current C#. And I doubt that it
will be possible in any of the future versions of C#.

It does not really fit well with the C# model.

Arne
 
G

Greg Young

Jon I am going to reply even though you have trashed everyone who has tried
to help you .. bad vibes dude.

You can get some of the information you are looking for (well the runtime
stuff)

In order to do this you will need to use the debugger API. Luckily for you
someone has already done just this with mdbg
http://blogs.msdn.com/jmstall/archive/2004/09/30/236281.aspx includes a
sample. For some of the other information you could do pre-processer based
parsing but for others (such as addresses of variables) you need runtime
based support and you surely don't want to pin everything so you don't have
to worry about the gc rearranging things.

You may also want to take a look at SOS in the debugging toolkit as it
already has alot of the functionality you are asking for.

Cheers,

Greg
 
J

Jon Slaughter

Greg Young said:
Jon I am going to reply even though you have trashed everyone who has
tried to help you .. bad vibes dude.

Well, sorry if you don't like it. I just feel that some of them deserve it.
See how simple and helpful your comments were? I'm sorry but I just don't
take crap from some of these people because I know how they are. I'm around
them all the time. They think that if you ask for help then they can march
in and try and treat you like an idiot so they can make themselfs look
smarter. I'm not saying they do this consciously or explicitly but some of
them do it. When I ask a question I don't want any of that crap. Your post
does not contain a hit of it. If you say stuff like "I don't see why you
need it. I never don't need it" then theres something else here besides
objective help. Farther comments like that then support my impression and
I'll act just like them. I don't mean to cause any problems for the rest of
you but I kinda feel its my mission to try my best to put these people in
there place. Every once in a while I might be wrong and I'll take the blame
for it but thats the price you gotta pay.

You can get some of the information you are looking for (well the runtime
stuff)

In order to do this you will need to use the debugger API. Luckily for you
someone has already done just this with mdbg
http://blogs.msdn.com/jmstall/archive/2004/09/30/236281.aspx includes a
sample. For some of the other information you could do pre-processer based
parsing but for others (such as addresses of variables) you need runtime
based support and you surely don't want to pin everything so you don't
have to worry about the gc rearranging things.

You may also want to take a look at SOS in the debugging toolkit as it
already has alot of the functionality you are asking for.

I though it might be somewhere in there but I couldn't find it.

Thanks for your informative reply,
Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top