An objects Identifier

  • Thread starter Thread starter Rain
  • Start date Start date
R

Rain

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 ?????? } }
 
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.


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
 
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.


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
 
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
 
Rene said:
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.




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?
}
 
Back
Top