473,796 Members | 2,640 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is this. needed?

I'd like to know when exactly the this. keyword is needed. For
example:

public class Combination
{
private long n = 0;
private long k = 0;
private long[] data = null;

public Combination(lon g n, long k)
{
if (n < 0 || k < 0) // normally n >= k
throw new Exception("Nega tive parameter in constructor");
this.n = n;
this.k = k;
this.data = new long[k];
for (long i = 0; i < k; ++i)
this.data[i] = i;
}
}

In the above code, I can see how this. may differentiate my class
scoped vars vs. parameters. If the class vars would have been nn and
kk instead of n and k, would this. still have been needed?

I've always viewed this. as useful for intelliparse but ultimately just
additional parsing. So I remove it.

Thanks,
Brett

Nov 17 '05 #1
8 1410

"Brett Romero" <ac*****@cygen. com> wrote in message
news:11******** *************@o 13g2000cwo.goog legroups.com...
I'd like to know when exactly the this. keyword is needed. For
example:

public class Combination
{
private long n = 0;
private long k = 0;
private long[] data = null;

public Combination(lon g n, long k)
{
if (n < 0 || k < 0) // normally n >= k
throw new Exception("Nega tive parameter in constructor");
this.n = n;
this.k = k;
this.data = new long[k];
for (long i = 0; i < k; ++i)
this.data[i] = i;
}
}

In the above code, I can see how this. may differentiate my class
scoped vars vs. parameters. If the class vars would have been nn and
kk instead of n and k, would this. still have been needed?
No. The this in this case simply tells the compiler you mean the field, not
the local parameter.
I've always viewed this. as useful for intelliparse but ultimately just
additional parsing. So I remove it.


Don't worry about additional parsing, worry about what makes sense to *YOU*,
compiler performance is hardly that important.
Nov 17 '05 #2
You don't generally need "this" when you're referring to your own
properties, variables, and other members, but it can help to make the
code more readable, or even to disambiguate if you're unfortunate
enough to have several possibilities with the same name.

As far as I know, the only time when you *really* need to use "this"
is when you aren't accessing any specific member of the current object
but merely the object itself, e.g.

void Print()
{
Console.Write(t his);
}

P.
Nov 17 '05 #3
In the code example, if I don't use this., how does the compiler know I
want to assign the parameters to the class level vars or just to
themselves?

Nov 17 '05 #4
Why not try the documentation for a change? C# keywords [1].

<%= Clinton Gallagher
METROmilwaukee (sm) "A Regional Information Service"
NET csgallagher AT metromilwaukee. com
URL http://metromilwaukee.com/
URL http://clintongallagher.metromilwaukee.com/
[1]
http://msdn.microsoft.com/library/de...eywords_pg.asp
"Brett Romero" <ac*****@cygen. com> wrote in message
news:11******** *************@o 13g2000cwo.goog legroups.com...
I'd like to know when exactly the this. keyword is needed. For
example:

public class Combination
{
private long n = 0;
private long k = 0;
private long[] data = null;

public Combination(lon g n, long k)
{
if (n < 0 || k < 0) // normally n >= k
throw new Exception("Nega tive parameter in constructor");
this.n = n;
this.k = k;
this.data = new long[k];
for (long i = 0; i < k; ++i)
this.data[i] = i;
}
}

In the above code, I can see how this. may differentiate my class
scoped vars vs. parameters. If the class vars would have been nn and
kk instead of n and k, would this. still have been needed?

I've always viewed this. as useful for intelliparse but ultimately just
additional parsing. So I remove it.

Thanks,
Brett

Nov 17 '05 #5

"Brett Romero" <ac*****@cygen. com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. .
In the code example, if I don't use this., how does the compiler know I
want to assign the parameters to the class level vars or just to
themselves?


You use 'this'. I was responding to your question about needing this if the
fields had been called nn and kk. More commonly you use properties and dont'
run into naming issues.
Nov 17 '05 #6
this makes no difference, except to the developer. At compile time, it
becomes that, and is not used any more. Remember that this is programmer
code, not executable code. that, on the other hand, is executable code. You
can't read that, but the computer can't read this. Of course, these are only
words that I wrote this afternoon. Those, on the other hand, are that which
is neither this nor these. And that's that!

And then some.

--
;-),

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

"Brett Romero" <ac*****@cygen. com> wrote in message
news:11******** *************@o 13g2000cwo.goog legroups.com...
I'd like to know when exactly the this. keyword is needed. For
example:

public class Combination
{
private long n = 0;
private long k = 0;
private long[] data = null;

public Combination(lon g n, long k)
{
if (n < 0 || k < 0) // normally n >= k
throw new Exception("Nega tive parameter in constructor");
this.n = n;
this.k = k;
this.data = new long[k];
for (long i = 0; i < k; ++i)
this.data[i] = i;
}
}

In the above code, I can see how this. may differentiate my class
scoped vars vs. parameters. If the class vars would have been nn and
kk instead of n and k, would this. still have been needed?

I've always viewed this. as useful for intelliparse but ultimately just
additional parsing. So I remove it.

Thanks,
Brett

Nov 17 '05 #7
"clintonG" <cs*********@RE MOVETHISTEXTmet romilwaukee.com > wrote in message
news:eA******** ******@tk2msftn gp13.phx.gbl...
Why not try the documentation for a change? C# keywords [1].


We get MANY questions in this newsgroup that could easily be answered by looking at the
documentation or googling the topic. We get MANY posters who don't want to even want to ATTEMPT to
figure it out on their own.
We get many posters who don't even think of searching for an answer before asking a common question.

Brett obviously did some testing, came up with a well thought out question.
He thought he knew the answer, but wasn't sure, so...
He wrote up an example that clarifies the issue and he posted it here as a sanity check.

Why would you pick THIS posting to send a snide response to?????

Have you actually read the C# Keywords entry for "this".
It would NOT have answered his question.

Give the guy a break
Bill
Nov 17 '05 #8

"Brett Romero" wrote...
I'd like to know when exactly the this. keyword is needed.
For example:

public class Combination
{
private long n = 0;
private long k = 0;
private long[] data = null;

public Combination(lon g n, long k)
{
if (n < 0 || k < 0) // normally n >= k
throw new Exception("Nega tive parameter in constructor");
this.n = n;
this.k = k;
this.data = new long[k];
for (long i = 0; i < k; ++i)
this.data[i] = i;
}
}

In the above code, I can see how this. may differentiate my class
scoped vars vs. parameters. If the class vars would have been nn and
kk instead of n and k, would this. still have been needed?
Not needed, but...
I've always viewed this. as useful for intelliparse but ultimately just
additional parsing. So I remove it.


If there are name conflicts between instance variables on one hand and local
variables on the other hand, you'll need "this" to differentiate between
them.

In such a short example as you provided you can't see the obvious benefit of
using "this", but as a system grows larger, you'll probably need more
descriptive names on both instance variables and local variables.

When that occurs it might be very handy to just use the same names for
variables with the same "meaning" regardless whether it's a local or
instance variable, and voila, the need for "this" occurs...

I wouldn't remove it, as it gives important information for other developers
that might look at your code. With the "this" keyword it's always obvious
that it's referring to an instance variable.

Without it, it may be more difficult to spot in large methods.

There's also other uses of the this keyword, such as when you're passing a
reference to the instance itself:

otherObject.Som eThing(this);

It's also used as a keyword when declaring indexers:

public long this [int index]
{
get
{
return this.data[index];
}
set
{
this.data[index] = value;
}
}

// Bjorn A
Nov 17 '05 #9

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

Similar topics

1
1469
by: Positive Contrarian | last post by:
Am looking for a sort of a forum. What's different is that all postings would have one or more addressees, and postings wd be visible only to them and the originator. Unthreaded is OK. (No, mail won't do what I need.) Env is a moderately small number of users. I've dl'd and installed a number of PHP forums, but none so far have the noted capabilities.
13
1771
by: Joe Feldman | last post by:
This position is located in the South Bay Area in Northern California. If you are interested please send me your resume in a word .doc so that I can review it. If this does not look like a match, we have over 100 other open positions. Senior Member of Technical Staff/ Principal Engineer- Protocols Infrastructure
3
1168
by: JD | last post by:
I have a web site built with asp.net and I have been told that the only files I need on the webserver are the files in the bin folder and aspx files, I don't need the .vb or the .resx files on the webserver. Or do I need them on the webserver? And whatis a .resx file and what does it do? -- J. D.
52
3238
by: Julie | last post by:
I'm supporting an application at work. Below are some code segments that I can't understand how they work. First let me say, I would never code this standard. I'm just really creeped out that it works. Here's the setup. I have a function, called GetEmployeeCertifications, that is going to make a call to a SQL DB and return a result set. This module calls another function, called FillParameters, to determine if SQL parameters need to...
0
10242
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10200
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
10021
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
9061
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7558
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6800
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
5453
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.