473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Proper programming semantics?

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
Nov 4 '06 #1
4 1509
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 Slaughter" <Jo***********@Hotmail.comwrote in message
news:xj*****************@newssvr29.news.prodigy.ne t...
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

Nov 4 '06 #2

Jon Slaughter wrote:
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.

Nov 4 '06 #3
Hi Jon,

Its common uses are best explained here:

"this (C# Reference)"
http://msdn2.microsoft.com/en-us/lib...sz(VS.80).aspx

--
Dave Sexton

"Jon Slaughter" <Jo***********@Hotmail.comwrote in message
news:xj*****************@newssvr29.news.prodigy.ne t...
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

Nov 4 '06 #4
Scott M. <No****@NoSpam.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 5 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: G.Ashok | last post by:
Hi All, What is your weightage of the 3 characteristics of Object-Oriented Programming i.e. Inheritance, Encapsulation and Polymorphism. for a total of 100% Please quote your values and let's...
28
by: steve yee | last post by:
i think c should adapt c++ template standard, as well as namespace. if so, c can replace c++ in many cases.
30
by: Jakle | last post by:
I have been googling, but can seem to find out about C GUI libraries. My main platform is Windows, but it would be nice to find a cross platform library. I've been programming with php, which...
318
by: jacob navia | last post by:
Rcently I posted code in this group, to help a user that asked to know how he could find out the size of a block allocated with malloc. As always when I post something, the same group of people...
171
by: Raman | last post by:
Hi All, Here is a small Code, int main(void) { char *p=(char *) malloc(100); strcpy(p,"Test1234567890"); p=p+10; free(p);
17
by: CoreyWhite | last post by:
I bought this book years ago, when I was just learning C++. Since then I've gone through every math course offered at my college, taken courses on coding C & thinking in terms how how to make the...
56
by: valentin tihomirov | last post by:
{ int i = 2; } int i = 1; There is no 'i' defined in the 'parent' context from the moment of declaration on. So what is the problem? They tell us they pursue language simplicity. The rule "do...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.