473,699 Members | 2,702 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 "myClassObjectI d" which is calling
the object from within the object itself. ??

public string IdName { get{ return ?????? } }
Jun 27 '08 #1
5 1277
On Fri, 16 May 2008 14:59:34 -0700, Rain <me@myplace.com wrote:
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 "myClassObjectI d" 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*********@nn owslpianmk.comw rote in message
news:op******** *******@petes-computer.local. ..
On Fri, 16 May 2008 14:59:34 -0700, Rain <me@myplace.com wrote:
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 "myClassObjectI d" 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 ArgumentExcepti on(“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 “GetIdentifie rName()” that could be used so that when
you throw the error you can do something like the following:

throw new ArgumentExcepti on( GetIdentifierNa me(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*********@nn owslpianmk.comw rote in message
news:op******** *******@petes-computer.local. ..
On Fri, 16 May 2008 14:59:34 -0700, Rain <me@myplace.com wrote:
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 "myClassObjectI d" 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.com schreef in bericht
news:eo******** ******@TK2MSFTN GP04.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*********@nn owslpianmk.comw rote in message
news:op******** *******@petes-computer.local. ..
On Fri, 16 May 2008 14:59:34 -0700, Rain <me@myplace.com wrote:
>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 "myClassObjectI d" 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 ArgumentExcepti on(“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 “GetIdentifie rName()” that could be used so that
when you throw the error you can do something like the following:

throw new ArgumentExcepti on( GetIdentifierNa me(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*********@nn owslpianmk.comw rote in message
news:op******** *******@petes-computer.local. ..
On Fri, 16 May 2008 14:59:34 -0700, Rain <me@myplace.com wrote:
>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 "myClassObjectI d" 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(Int3 2 x)
{
MyOtherFunction (x);
}

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

--
Lasse Vågsæther Karlsen
mailto:la***@vk arlsen.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
2039
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 Object. There is no UBC in C++, nor will there ever be one (well, there actually /is/ a UBC in C++, but it is a pure abstraction). One feature of user defined Java classes is that they all have a member derived from the java.lang.Class object. The...
115
4885
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 function "func()", can i say that ptr is pointing to some C object ? Is a C object always associated with some "data" ? thanx in advance for any help .....
6
4315
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 to it. I've gotten as far as adding a reference to the TAPI3.dll, which gets me all the type information, but now I'm stuck in trying to figure out how to create a TAPI object. In my WinMain, I figured I'd just have to do TAPIClass myObject=new...
7
12888
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 compare these two objects so that it will return true if both object's members have the same value... it is good if u can give me a single function or simple code snippet.. Thank U -- Peter...
2
46987
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 this.city=city this.state=state this.zip=zip
1
6333
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 should be automatically generated for each new object. One more requirement is keeping the objects in the order in which they were inserted, not sorted by their ID. Considering the following options of implementation, which would be the best...
1
1586
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 class I get the error that is in the comment. This is really bothering me as I spent the last hour and a half with a classmate working on this and we can not figure out whats up. class Person { public: Person(int sinNumber, char studentName);...
31
3188
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 some of the most powerful programming tools I have found. Often times as my program grows so do my objects. Often times I look back and see that my objects could be broken down int several smaller more re-usable module pieces of code. Is it a...
2
1877
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 differences between delegates and the use of pointers to objects, I'm nonetheless a little confused by how similar the syntax used for references to methods is to that used for references to objects. Recently at a few different forums I posted a brief...
0
8613
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9172
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8908
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7745
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4374
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.