Sign in to follow this  
Stranger

[.net] The "this" operator?

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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").

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
you don't need to write "this.Close()"

just write "Close()"

"this." is only used for disambiguation between member variables and local ones

Share this post


Link to post
Share on other sites
"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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this