By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,084 Members | 1,255 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,084 IT Pros & Developers. It's quick & easy.

An objects Identifier

P: n/a
Is there a way for an object to know the identifier which has called it

MyClass myClassObjectId = new MyClass();

how can I get the name of the identifier "myClassObjectId" which is calling
the object from within the object itself. ??

public string IdName { get{ return ?????? } }
Jun 27 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Fri, 16 May 2008 14:59:34 -0700, Rain <me@myplace.comwrote:
Is there a way for an object to know the identifier which has called it

MyClass myClassObjectId = new MyClass();

how can I get the name of the identifier "myClassObjectId" which is
calling
the object from within the object itself. ??

public string IdName { get{ return ?????? } }
The only way that I can think of would be to use reflection to inspect the
call stack, then the line of code making the call, to extract the variable
(be it a local or class field) referencing the instance on which the
method was called.

But surely the likelihood of this being a correct design is extremely low.

What exactly are you trying to accomplish? We know the implementation you
have in mind, but what actual problem are you trying to solve? It seems
doubtful that learning the identifier of the variable referencing the
instance used to call some particular instance code is really a good thing
to be doing, generally.

Pete
Jun 27 '08 #2

P: n/a
Hi Peter,

The only thing I was trying to accomplish was to satisfy an academic
curiosity. I was writing some code using delegates and wanted to demonstrate
something using delegates which were attached to instance member functions,
this is what generated the question. In the end I wrote out the hash code
for the object's being used.

As I say, simply a curiosity.

Thanks for your help.
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Fri, 16 May 2008 14:59:34 -0700, Rain <me@myplace.comwrote:
Is there a way for an object to know the identifier which has called it

MyClass myClassObjectId = new MyClass();

how can I get the name of the identifier "myClassObjectId" which is
calling
the object from within the object itself. ??

public string IdName { get{ return ?????? } }
The only way that I can think of would be to use reflection to inspect the
call stack, then the line of code making the call, to extract the variable
(be it a local or class field) referencing the instance on which the
method was called.

But surely the likelihood of this being a correct design is extremely low.

What exactly are you trying to accomplish? We know the implementation you
have in mind, but what actual problem are you trying to solve? It seems
doubtful that learning the identifier of the variable referencing the
instance used to call some particular instance code is really a good thing
to be doing, generally.

Pete
Jun 27 '08 #3

P: n/a
Hi Pete:

I can think of a particular time when it would be kind of nice to get that
type of information. For example, given the function below:

public void MyFunction(int someArg)
{
If (someArg == 0)
throw new ArgumentException(“someArg cant be equal to zero”);
}

What would happen if I changed the name of “someArg” to something else like
“myArg”? If I do that then I would have to make sure I update the Exception
description too.

I don’t want to be critical about this but it would be nice if there was a
compiler method like “GetIdentifierName()” that could be used so that when
you throw the error you can do something like the following:

throw new ArgumentException( GetIdentifierName(someArg) + “cant be equal to
zero”);

This way, if I was to change the name of the argument to “myArg” I would get
a compile error but chances are that will never happen because most people
would refactor the code and everything would be take care of automatically.

Cheers.
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Fri, 16 May 2008 14:59:34 -0700, Rain <me@myplace.comwrote:
Is there a way for an object to know the identifier which has called it

MyClass myClassObjectId = new MyClass();

how can I get the name of the identifier "myClassObjectId" which is
calling
the object from within the object itself. ??

public string IdName { get{ return ?????? } }
The only way that I can think of would be to use reflection to inspect the
call stack, then the line of code making the call, to extract the variable
(be it a local or class field) referencing the instance on which the
method was called.

But surely the likelihood of this being a correct design is extremely low.

What exactly are you trying to accomplish? We know the implementation you
have in mind, but what actual problem are you trying to solve? It seems
doubtful that learning the identifier of the variable referencing the
instance used to call some particular instance code is really a good thing
to be doing, generally.

Pete

Jun 27 '08 #4

P: n/a
Rain,

I always write that as this kind of things are realy needed (not like you to
statisfy an academic curiosity) you should start to rewrite your solution
(it becomes not any more maintainable)..

Just my opinion.

Cor

"Rain" <me@myplace.comschreef in bericht
news:eo**************@TK2MSFTNGP04.phx.gbl...
Hi Peter,

The only thing I was trying to accomplish was to satisfy an academic
curiosity. I was writing some code using delegates and wanted to
demonstrate something using delegates which were attached to instance
member functions, this is what generated the question. In the end I wrote
out the hash code for the object's being used.

As I say, simply a curiosity.

Thanks for your help.
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Fri, 16 May 2008 14:59:34 -0700, Rain <me@myplace.comwrote:
>Is there a way for an object to know the identifier which has called it

MyClass myClassObjectId = new MyClass();

how can I get the name of the identifier "myClassObjectId" which is
calling
the object from within the object itself. ??

public string IdName { get{ return ?????? } }

The only way that I can think of would be to use reflection to inspect the
call stack, then the line of code making the call, to extract the variable
(be it a local or class field) referencing the instance on which the
method was called.

But surely the likelihood of this being a correct design is extremely low.

What exactly are you trying to accomplish? We know the implementation you
have in mind, but what actual problem are you trying to solve? It seems
doubtful that learning the identifier of the variable referencing the
instance used to call some particular instance code is really a good thing
to be doing, generally.

Pete
Jun 27 '08 #5

P: n/a
Rene wrote:
Hi Pete:

I can think of a particular time when it would be kind of nice to get
that type of information. For example, given the function below:

public void MyFunction(int someArg)
{
If (someArg == 0)
throw new ArgumentException(“someArg cant be equal to zero”);
}

What would happen if I changed the name of “someArg” to something else
like “myArg”? If I do that then I would have to make sure I update the
Exception description too.

I don’t want to be critical about this but it would be nice if there was
a compiler method like “GetIdentifierName()” that could be used so that
when you throw the error you can do something like the following:

throw new ArgumentException( GetIdentifierName(someArg) + “cant be equal
to zero”);

This way, if I was to change the name of the argument to “myArg” I would
get a compile error but chances are that will never happen because most
people would refactor the code and everything would be take care of
automatically.

Cheers.
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Fri, 16 May 2008 14:59:34 -0700, Rain <me@myplace.comwrote:
>Is there a way for an object to know the identifier which has called it

MyClass myClassObjectId = new MyClass();

how can I get the name of the identifier "myClassObjectId" which is
calling
the object from within the object itself. ??

public string IdName { get{ return ?????? } }

The only way that I can think of would be to use reflection to inspect the
call stack, then the line of code making the call, to extract the variable
(be it a local or class field) referencing the instance on which the
method was called.

But surely the likelihood of this being a correct design is extremely low.

What exactly are you trying to accomplish? We know the implementation you
have in mind, but what actual problem are you trying to solve? It seems
doubtful that learning the identifier of the variable referencing the
instance used to call some particular instance code is really a good thing
to be doing, generally.

Pete
The problem with this line of logic is that the parameters are not
"variables", they're expressions that results from reading the contents
of variables.

The problem with that is that this is legal:

MyFunction(0)

in which case you would have no variable and your error message would
either be (if this was allowed)

"0 cannot be equal to zero"
or
" cannot be equal to zero"

Your best bet is to take note of the stack trace and go backwards in the
source code to find the place where you actually have a variable. The
variable could be multiple levels up in which case it would make no
sense to report the parameter name from the previous method layer.

For instance

Int32 z = 0;
MyFunction(z)

private static void MyFunction(Int32 x)
{
MyOtherFunction(x);
}

private static void MyOtherFunction(Int32 y)
{
if (y == 0) ... what to report here?
}

--
Lasse Vågsæther Karlsen
mailto:la***@vkarlsen.no
http://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3
Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.