Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. What is "this"

What is "this"

Scheduled Pinned Locked Moved C#
data-structuresquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    urbane tiger
    wrote on last edited by
    #1

    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

    E B 2 Replies Last reply
    0
    • U urbane tiger

      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

      E Offline
      E Offline
      Esmo2000
      wrote on last edited by
      #2

      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!

      U 1 Reply Last reply
      0
      • E Esmo2000

        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!

        U Offline
        U Offline
        urbane tiger
        wrote on last edited by
        #3

        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

        R 1 Reply Last reply
        0
        • U urbane tiger

          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

          R Offline
          R Offline
          Robert Rohde
          wrote on last edited by
          #4

          Hi, you are looking for the StackTrace[^] class. Just create a new instance and will contain all information you need. Robert

          U 1 Reply Last reply
          0
          • R Robert Rohde

            Hi, you are looking for the StackTrace[^] class. Just create a new instance and will contain all information you need. Robert

            U Offline
            U Offline
            urbane tiger
            wrote on last edited by
            #5

            Thank you robert - I knew it had to be there somewhere MS rarely let developers down :)

            1 Reply Last reply
            0
            • U urbane tiger

              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

              B Offline
              B Offline
              BoneSoft
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups