473,795 Members | 3,457 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1528
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.comwrot e in message
news:xj******** *********@newss vr29.news.prodi gy.net...
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.comwrot e in message
news:xj******** *********@newss vr29.news.prodi gy.net...
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.co m>
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
1815
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 discuss how the people thinking about OOP :-) Regards,
28
2642
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
32268
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 has a C/C++ syntax structure, but want to move on to compliled languages. I was all ready to go with C++ and wxWindows, but I'm applying for an entry level programming possition. They use their own propriatary language, with
318
13082
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 started to try to find possible errors, a harmless passtime they seem to enjoy. One of their remarks was that I used "int" instead of "size_t" for the input of my allocator function.
171
4956
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
4726
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 smallest tightest algorithms to preform specific functions. I've also grown and matured a lot, and am wiser and older. I'm reading through the C+ + Programming Language, Third Edition now, and I can actually understand it. I can understand it...
56
5750
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 not define a variable more than once in the same context" is natural, and simplest therefore. All normal languages obey it therefore. Overcomplicating a grammar by injecting more barrieres is a path
0
9672
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10437
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10001
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3723
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.