O'Reilly Forums: "this" Confusion- When To Use. - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

"this" Confusion- When To Use. when is "THIS" required and when is it better not to reference

#1 User is offline   ELO~ 

  • New Member
  • Pip
  • Group: Members
  • Posts: 6
  • Joined: 17-August 12

Posted 23 August 2012 - 06:42 AM

I am working on the Lab Day at the Races- what is unclear to me and reading other posts it isnt getting any clearer- when is THIS required.

example... if RUN() method is in the Greyhound class- which as a MyPictureBox field- and I have an object dog[0].
then in the run method do I really have to reference the location as
this.MyPictureBox.Location or could it just be MyPictureBox.Location?

everything within the dog Greyhound class is related to the object- therefore the refernce to the MyPictureBox would be for dog[0] correct? so adding a "this" would be redundant? or am I mis-thinking this whole thing?

or is there a 'standard' that states it is better to use the "this" in ~this B) ~ situation?

thanks for any insight...
0

#2 User is offline   Josh_T 

  • New Member
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 31-January 13

Posted 02 February 2013 - 05:44 AM

View PostELO~, on 23 August 2012 - 06:42 AM, said:

I am working on the Lab Day at the Races- what is unclear to me and reading other posts it isnt getting any clearer- when is THIS required.

example... if RUN() method is in the Greyhound class- which as a MyPictureBox field- and I have an object dog[0].
then in the run method do I really have to reference the location as
this.MyPictureBox.Location or could it just be MyPictureBox.Location?

everything within the dog Greyhound class is related to the object- therefore the refernce to the MyPictureBox would be for dog[0] correct? so adding a "this" would be redundant? or am I mis-thinking this whole thing?

or is there a 'standard' that states it is better to use the "this" in ~this B) ~ situation?

thanks for any insight...



Someone may add to this but here goes:
This distinguishes between variables as to remove ambiguity between same named variables. So if you are dealing with a public form-wide variable and a class variable of the same name, you can, within the class, use this.{variable} to distinguish between the form-wide variable and the class variable. The this keyword also scopes you to the current class.

See also:
http://stackoverflow...se-this-keyword
http://stackoverflow...use-this-method
http://stackoverflow...he-this-keyword
...and many more articles if you search google for "c# when to use this".


I hope this helped.

This post has been edited by Josh_T: 02 February 2013 - 05:44 AM

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users