Proper programming semantics?

  • Thread starter Thread starter Jon Slaughter
  • Start date Start date
J

Jon Slaughter

When programming, say, a control should I use the objects of the control
directly, reference them from this, or use, if possible, the ones passed
through event arguments?

e.g., in a paint event I can use this. or just access the fields and methods
of the control directly or use the painteventargs argument passed. Say I
want to draw a line on the control.

What I'm a little confused about is when to use "this.". I know its mainly
used for qualification on the current instance of the object but is there
any other reason to use it? And is there any reason to use the event args
when one can use the current instance directly?

Thanks,
Jon
 
Using "this" or not results in the exact same result, so it doesn't matter.

Some folks like to use "this" because when you type the "." after "this",
you'll get intellSense helping you remember what the rest of your class's
members are and you'll actually wind up doing less typing with less chances
for a mistakenly referenced class member. Of course, there are other
shortcuts you can take to remind yourself of the names of your class members
besides using "this" (like beginning the member name and typing CTRL +
Space), so some people don't use "this".

The only time you must use "this" is when the class needs to refer to
instances of itself. Other than that, it's up to you which to use.
 
Jon said:
When programming, say, a control should I use the objects of the control
directly, reference them from this, or use, if possible, the ones passed
through event arguments?

e.g., in a paint event I can use this. or just access the fields and methods
of the control directly or use the painteventargs argument passed. Say I
want to draw a line on the control.

What I'm a little confused about is when to use "this.". I know its mainly
used for qualification on the current instance of the object but is there
any other reason to use it? And is there any reason to use the event args
when one can use the current instance directly?

As to whether to use the event argument (usually the first argument,
sender) or just reference the control directly, it doesn't matter
unless you have several controls' events routed to the same event
handler.

Some people, for example, prefer to reduce the number of event handler
methods by combining them where they do the same thing, and using the
"sender" argument to decide upon which control to act.
 
Scott M. said:
Using "this" or not results in the exact same result, so it doesn't matter.

Some folks like to use "this" because when you type the "." after "this",
you'll get intellSense helping you remember what the rest of your class's
members are and you'll actually wind up doing less typing with less chances
for a mistakenly referenced class member. Of course, there are other
shortcuts you can take to remind yourself of the names of your class members
besides using "this" (like beginning the member name and typing CTRL +
Space), so some people don't use "this".

The only time you must use "this" is when the class needs to refer to
instances of itself. Other than that, it's up to you which to use.

And also if you have a local variable available with the same name as
an instance variable. This can be common in constructors:

public class Person
{
string name;
int age;

public Person (string name, int age)
{
this.name = name;
this.age = age;
}
}

Some naming conventions prevent this, but others don't.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top