Using "this"
-
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?
-
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?
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 -
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?
this
refers to that particular instance of the class. In the snippet above you have to usethis.value
in order to assign the value passed in asvalue
to the variable calledvalue
in the class. If you didn't specifythis
the compiler wouldn't know that you mean the variable namedvalue
in the class instead of the local variable calledvalue
. Make a little more sense? James "It is self repeating, of unknown pattern" Data - Star Trek: The Next Generation -
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 GandhiNice 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
-
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 Gandhijdunlap 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 -
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 TwainBoth 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 -
this
refers to that particular instance of the class. In the snippet above you have to usethis.value
in order to assign the value passed in asvalue
to the variable calledvalue
in the class. If you didn't specifythis
the compiler wouldn't know that you mean the variable namedvalue
in the class instead of the local variable calledvalue
. Make a little more sense? James "It is self repeating, of unknown pattern" Data - Star Trek: The Next Generation -
So this code translates as: "Assign the value from parameter to the 'value' variable of the DBBool class". Am I right?
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