What is "this"
-
Just as all seems clear, something comes along that blows way one's fundemental assumptions about life etc. How can an object determine the type of object by which it was instanced - essentially who done the new that bought me to life. I assumed "this" was about the current instance, one of the things that annoys me about "this" it's baggage. I wanted to know the context in which a particular class member (a set accessor) was being used, and I thought a'ha there must be something in all that "this" baggage that will give me the name of the instancing class - but I can't find it - it seems I can get lots of info about "who" I is, but precious little about "where" I is. I thought I'd try walking the stack, but I can't find that either, not even 42.InstancingClass works mad: Squarks - PhilD
-
Just as all seems clear, something comes along that blows way one's fundemental assumptions about life etc. How can an object determine the type of object by which it was instanced - essentially who done the new that bought me to life. I assumed "this" was about the current instance, one of the things that annoys me about "this" it's baggage. I wanted to know the context in which a particular class member (a set accessor) was being used, and I thought a'ha there must be something in all that "this" baggage that will give me the name of the instancing class - but I can't find it - it seems I can get lots of info about "who" I is, but precious little about "where" I is. I thought I'd try walking the stack, but I can't find that either, not even 42.InstancingClass works mad: Squarks - PhilD
You probably want to know about reflection. http://my.execpc.com/~gopalan/dotnet/reflection.html . This is a pointer to the current object. It's not baggage because it is useful for all sorts of function calls referring to the object itself (among other things the compiler must have it for). "I wanted to know the context in which a particular class member (a set accessor) was being used, and I thought a'ha there must be something in all that "this" baggage that will give me the name of the instancing class - but I can't find it - it seems I can get lots of info about "who" I is, but precious little about "where" I is." Do you mean you want to know which class was calling this object? That's more difficult, and usually if your using reflection, it means you did something wrong anyways. Try redesigning your code to avoid it. J Did I post well? Rate it! Did I post badly? Rate that too!
-
You probably want to know about reflection. http://my.execpc.com/~gopalan/dotnet/reflection.html . This is a pointer to the current object. It's not baggage because it is useful for all sorts of function calls referring to the object itself (among other things the compiler must have it for). "I wanted to know the context in which a particular class member (a set accessor) was being used, and I thought a'ha there must be something in all that "this" baggage that will give me the name of the instancing class - but I can't find it - it seems I can get lots of info about "who" I is, but precious little about "where" I is." Do you mean you want to know which class was calling this object? That's more difficult, and usually if your using reflection, it means you did something wrong anyways. Try redesigning your code to avoid it. J Did I post well? Rate it! Did I post badly? Rate that too!
Totally agree, a member should not need to know its instancing class - what I have to do is to log events based on the values of the objects properties, the most sensible place to do that is in the object set accessor. In addition to logging the objects properties, I am also required to log the event context, what better way than to include the instancing class tree, the defining class tree is of little consequence in this scenario. And we'd prefer that the instancing classes not know that the objects are being monitored. The annoying thing is that I can see all the information I want- its in the stack window. I'll read Gopalan's article - I've been trying to avoid knowing about Reflection, not on principle, but because my brain has a limited learning capacity, on the threshold of which I currently exist. Thanks for your response - rgds PhilD
-
Totally agree, a member should not need to know its instancing class - what I have to do is to log events based on the values of the objects properties, the most sensible place to do that is in the object set accessor. In addition to logging the objects properties, I am also required to log the event context, what better way than to include the instancing class tree, the defining class tree is of little consequence in this scenario. And we'd prefer that the instancing classes not know that the objects are being monitored. The annoying thing is that I can see all the information I want- its in the stack window. I'll read Gopalan's article - I've been trying to avoid knowing about Reflection, not on principle, but because my brain has a limited learning capacity, on the threshold of which I currently exist. Thanks for your response - rgds PhilD
Hi, you are looking for the StackTrace[^] class. Just create a new instance and will contain all information you need. Robert
-
Hi, you are looking for the StackTrace[^] class. Just create a new instance and will contain all information you need. Robert
Thank you robert - I knew it had to be there somewhere MS rarely let developers down :)
-
Just as all seems clear, something comes along that blows way one's fundemental assumptions about life etc. How can an object determine the type of object by which it was instanced - essentially who done the new that bought me to life. I assumed "this" was about the current instance, one of the things that annoys me about "this" it's baggage. I wanted to know the context in which a particular class member (a set accessor) was being used, and I thought a'ha there must be something in all that "this" baggage that will give me the name of the instancing class - but I can't find it - it seems I can get lots of info about "who" I is, but precious little about "where" I is. I thought I'd try walking the stack, but I can't find that either, not even 42.InstancingClass works mad: Squarks - PhilD
Why don't you have the instantiating class supply the necessary data to the instance? Unless it's methods that you're after, that if needed to be shared could be in another class and/or static.
this
only provides a reference to the current instance of the class. It will only access members of that instance. Generally speaking, a class holds necessary data, and performs necessary functions based on that data, to represent a finite piece of the problem domain. If you're class doesn't have all the data it needs, you have a design problem. It shouldn't need to look elsewhere for what it needs.
Try code model generation tools at BoneSoft.com.