473,230 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,230 software developers and data experts.

An objects Identifier

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
5 1256
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Steven T. Hatton | last post by:
I've had an idea kicking around in my head regarding how to create a library of classes (templates?) that provide the same kind of functionality as do Java classes which all derive from the UBC...
115
by: junky_fellow | last post by:
What is a C object ? If i have some function "func()" in my C program, then can i say that "func()" is a C object ? or if i have some function pointer (ptr) which contains the address of...
6
by: Dan | last post by:
Sounds simple enough - problem is there seems to be WAY too much documentation and can't seem to find the right one. I created a VC++ .NET Windows Form application, and I'm trying to add TAPI3...
7
by: Prabhudhas Peter | last post by:
I have two object instances of a same class... and i assigned values in both object instances (or the values can be taken from databse and assigned to the members of the objects)... Now i want to...
2
by: wlevine | last post by:
Can someone give me an example of how to construct an array of objects? for example, I have: function objFacility(id,name,adr,city,state,zip) { this.id = id this.name=name this.adr=adr...
1
by: Danny Liberty | last post by:
I need some opionions on an issue here... Suppose I want to keep a collection of objects, each need to be uniquely identified by a number. This number has no meaning as long as it's unique, so it...
1
by: dav3 | last post by:
Any help here is appreciated folks. First in my Person class the comments = errors visual basics is giving me and I am not sure why. Also when i try and set up my array of pointers to Student...
31
by: JoeC | last post by:
I have read books and have ideas on how to create objects. I often create my own projects and programs. They end up getting pretty complex and long. I often use objects in my programs they are...
2
by: BobLewiston | last post by:
The concept of delegates (references to methods) per se is new to me (although I used function pointers in C years ago), and although the literature acknowledges that there are significant...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.