[.net] The "this" operator?

Started by
6 comments, last by jods 18 years, 9 months ago
Can I have your help on this operator? With this operator I can use methods of a class in the class itself? I have in Visual Studio the command this.Close() to close the form but it doesnt work with Form.Close() so where is the difference? In other words give me a detailed explanation of the this keyword. Thank you for reading.
Advertisement
I don't know much about the .net languages, but I'd assume 'this' refers to the current instance of the class, instead of the type of class. Member functions need to know what instance they're operating on to retrieve and set member variables, as well as call other member functions.
Quote:Original post by bytecoder
I don't know much about the .net languages, but I'd assume 'this' refers to the current instance of the class, instead of the type of class. Member functions need to know what instance they're operating on to retrieve and set member variables, as well as call other member functions.


this poster is correct

(notice that my sentence would not have made sense if I had said "Human is correct").

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

Quote:Original post by Stranger
Can I have your help on this operator? With this operator I can use methods of a class in the class itself? I have in Visual Studio the command this.Close() to close the form but it doesnt work with Form.Close() so where is the difference? In other words give me a detailed explanation of the this keyword. Thank you for reading.


If you call Form.Close(), it's assumed that Form.Close() is a static member function of the Form class. However, it's not.

As already explained, this refers to the current object(Visual Basic used 'me'). All operations with this.something are executed on the concurrent object.

Toolmaker

'this' is not an operator, it is an identifier.
Quote:Original post by capn_midnight
Quote:Original post by bytecoder
I don't know much about the .net languages, but I'd assume 'this' refers to the current instance of the class, instead of the type of class. Member functions need to know what instance they're operating on to retrieve and set member variables, as well as call other member functions.


this poster is correct

(notice that my sentence would not have made sense if I had said "Human is correct").


Haha, nice one [lol]
you don't need to write "this.Close()"

just write "Close()"

"this." is only used for disambiguation between member variables and local ones
Graphics make the game! 8-)
"this" can also be used when you need to pass the current instance to a function, or use it in an expression, like:

OtherClass.DoSomething(this);

or

newNode.parent = this;

Otherwise, using this.something is usually a disambiguation, just like Nexus said.

This topic is closed to new replies.

Advertisement