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. Using "this"

Using "this"

Scheduled Pinned Locked Moved C#
csharptutorialquestion
8 Posts 6 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.
  • K Offline
    K Offline
    kensai
    wrote on last edited by
    #1

    Hi. I'm a C# newbie and I'm having a bit difficult time to understand using "this". For example: public class DBBool { public DBBool(int value) { this.value = value; } ..... } By using "this.value = .." to what do we assing the value and how will we use that value afterwards?

    J J 2 Replies Last reply
    0
    • K kensai

      Hi. I'm a C# newbie and I'm having a bit difficult time to understand using "this". For example: public class DBBool { public DBBool(int value) { this.value = value; } ..... } By using "this.value = .." to what do we assing the value and how will we use that value afterwards?

      J Offline
      J Offline
      J Dunlap
      wrote on last edited by
      #2

      The this pointer is a reference to "yourself". Use it to access members (properties and methods) you inherited from other classes, and for a way to pass a ref of yourself to another object.

      "Do unto others as you would have them do unto you." - Jesus
      "An eye for an eye only makes the whole world blind." - Mahatma Gandhi

      D N 2 Replies Last reply
      0
      • K kensai

        Hi. I'm a C# newbie and I'm having a bit difficult time to understand using "this". For example: public class DBBool { public DBBool(int value) { this.value = value; } ..... } By using "this.value = .." to what do we assing the value and how will we use that value afterwards?

        J Offline
        J Offline
        James T Johnson
        wrote on last edited by
        #3

        this refers to that particular instance of the class. In the snippet above you have to use this.value in order to assign the value passed in as value to the variable called value in the class. If you didn't specify this the compiler wouldn't know that you mean the variable named value in the class instead of the local variable called value. Make a little more sense? James "It is self repeating, of unknown pattern" Data - Star Trek: The Next Generation

        K 1 Reply Last reply
        0
        • J J Dunlap

          The this pointer is a reference to "yourself". Use it to access members (properties and methods) you inherited from other classes, and for a way to pass a ref of yourself to another object.

          "Do unto others as you would have them do unto you." - Jesus
          "An eye for an eye only makes the whole world blind." - Mahatma Gandhi

          D Offline
          D Offline
          David Stone
          wrote on last edited by
          #4

          Nice sig...:cool:


          Hawaian shirts and shorts work too in Summer. People assume you're either a complete nut (in which case not a worthy target) or so damn good you don't need to worry about camouflage... -Anna-Jayne Metcalfe on Paintballing

          1 Reply Last reply
          0
          • J J Dunlap

            The this pointer is a reference to "yourself". Use it to access members (properties and methods) you inherited from other classes, and for a way to pass a ref of yourself to another object.

            "Do unto others as you would have them do unto you." - Jesus
            "An eye for an eye only makes the whole world blind." - Mahatma Gandhi

            N Offline
            N Offline
            Nick Seng
            wrote on last edited by
            #5

            jdunlap wrote: "Do unto others as you would have them do unto you." - Jesus Not to sound picky, but I thought that was quoted by confucious?? Notorious SMC


            The difference between the almost-right word & the right word is a really large matter - it's the difference between the lightning bug and the Lightning Mark Twain
            Get your facts first, and then you can distort them as much as you please Mark Twain

            J 1 Reply Last reply
            0
            • N Nick Seng

              jdunlap wrote: "Do unto others as you would have them do unto you." - Jesus Not to sound picky, but I thought that was quoted by confucious?? Notorious SMC


              The difference between the almost-right word & the right word is a really large matter - it's the difference between the lightning bug and the Lightning Mark Twain
              Get your facts first, and then you can distort them as much as you please Mark Twain

              J Offline
              J Offline
              J Dunlap
              wrote on last edited by
              #6

              Both of them said it. It is the basic tenet of morality - loving and caring for others. But you are right - that exact wording was probably Confucius's. It will be different depending on which translation of the Bible you use, but here is the NASB-U version, which puts it in plain, every-day English: Luke 6:31 "Treat others the same way you would want them to treat you." Something to think about from day to day, isn't it? :)

              "Do unto others as you would have them do unto you." - Jesus
              "An eye for an eye only makes the whole world blind." - Mahatma Gandhi

              1 Reply Last reply
              0
              • J James T Johnson

                this refers to that particular instance of the class. In the snippet above you have to use this.value in order to assign the value passed in as value to the variable called value in the class. If you didn't specify this the compiler wouldn't know that you mean the variable named value in the class instead of the local variable called value. Make a little more sense? James "It is self repeating, of unknown pattern" Data - Star Trek: The Next Generation

                K Offline
                K Offline
                kensai
                wrote on last edited by
                #7

                So this code translates as: "Assign the value from parameter to the 'value' variable of the DBBool class". Am I right?

                L 1 Reply Last reply
                0
                • K kensai

                  So this code translates as: "Assign the value from parameter to the 'value' variable of the DBBool class". Am I right?

                  L Offline
                  L Offline
                  Leon van Wyk
                  wrote on last edited by
                  #8

                  Short and Sweet: "this" refers to the class in which you are typing the word "this" in. It just specifies that you are working with THIS class and not any other class the you refernse to, just the one were you are currently coding in.... public class OtherClass { string MyVar = "OtherVar"; public OtherClass() { } } public class MyClass { string MyVar = "YourVar"; public MyClass() { string OUTCOME = this.MyVar //is anything in MyClass, nowhere else } } the OUTCOME would be "YourVar"; Leon v Wyk

                  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