By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,105 Members | 1,551 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,105 IT Pros & Developers. It's quick & easy.

Recommendations on using the "this" keywords?

P: n/a
Hello!

I'm wondering what the recommendations are for using the "this" keyword?

The problem is that I really like to use the "this" keyword, but it tends to
make the code more verbose (which some don't like). Your oppinion please?
...

internal void PageDeleted(Page page)
{
if (this.pages == null) { return; }
this.pages.InnerList.Remove(page.PageID);
}

Thanks in advance - it's greatly appreciated.

--
venlig hilsen / with regards
anders borum
--
Nov 16 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
I use it only when there is a possible conflict, e.g.

public void DoSomethingToPage(Page page)
{
//blah blah blah
this.page = page;
}

The form designers probably write this.everything to avoid this sort of
thing without having to parse the code in order to do it.
--
Pete
====
Audio compression components, DIB graphics controls, FastStrings
http://www.droopyeyes.com

Read or write articles on just about anything
http://www.HowToDoThings.com
Nov 16 '05 #2

P: n/a
In article <#P**************@TK2MSFTNGP09.phx.gbl>, a@b.dk says...
Hello!

I'm wondering what the recommendations are for using the "this" keyword?

The problem is that I really like to use the "this" keyword, but it tends to
make the code more verbose (which some don't like). Your oppinion please?
..

internal void PageDeleted(Page page)
{
if (this.pages == null) { return; }
this.pages.InnerList.Remove(page.PageID);
}

Thanks in advance - it's greatly appreciated.

I used it alot in VS 2002/2003 as it brought up the intellisense however
as of vs 2005 the intellisense works as you type (which is nice) so I
would only use it as a conflict resolver.

--
Thanks
Mark
Nov 16 '05 #3

P: n/a
It's probably more important to use it consistently, than whether or not you
use it; but personally, I have come up with the following guidelines:

1) I use "this." to refer to fields or properties of the current object,
reasoning that it avoids any confusion about whether you are referring to a
local variable or to a member. I feel that being specific makes the code
more immediately readable to other (or to me, six months later).

2) I do NOT use "this." to call methods of the current object, because if I
am calling a method of some other object I'll have a reference (or a class
name, in the case of a static method) to prefix that call with. There is no
such thing in C# as a global function that's not associated with an object,
so there's nothing to confuse an internal method call with if you don't use
"this." Therefore I avoide "this." for method calls as all it seems to
accomplish is to make code more verbose.

--Bob

"Anders Borum" <a@b.dk> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hello!

I'm wondering what the recommendations are for using the "this" keyword?

The problem is that I really like to use the "this" keyword, but it tends to make the code more verbose (which some don't like). Your oppinion please?
..

internal void PageDeleted(Page page)
{
if (this.pages == null) { return; }
this.pages.InnerList.Remove(page.PageID);
}

Thanks in advance - it's greatly appreciated.

--
venlig hilsen / with regards
anders borum
--

Nov 16 '05 #4

P: n/a
Back with MFC I used to put m_ in front of the name of all my private class
fields. I went on doing this when I changed to .NET, but after a while I
found it didn't fit so well in .NET, specialy when dealing with inner
classes (since they have access to private members of the class they are
in). So I changed to using the this keyword instead of a m_ name prefix with
private fields. This is also more consistant with the code generated by VS.

Etienne Boucher

"Anders Borum" <a@b.dk> a écrit dans le message de
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hello!

I'm wondering what the recommendations are for using the "this" keyword?

The problem is that I really like to use the "this" keyword, but it tends to make the code more verbose (which some don't like). Your oppinion please?
..

internal void PageDeleted(Page page)
{
if (this.pages == null) { return; }
this.pages.InnerList.Remove(page.PageID);
}

Thanks in advance - it's greatly appreciated.

--
venlig hilsen / with regards
anders borum
--

Nov 16 '05 #5

P: n/a
Hello!

I would like to thank everybody for their suggestions and recommendations. I
am currently writing a set of guidelines (naming etc.) for our developers,
and want to make sure they are as objective and logic as possible.

Thanks again :-)

--
venlig hilsen / with regards
anders borum
--
Nov 16 '05 #6

P: n/a
SP

"Anders Borum" <a@b.dk> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hello!

I'm wondering what the recommendations are for using the "this" keyword?

The problem is that I really like to use the "this" keyword, but it tends
to
make the code more verbose (which some don't like). Your oppinion please?
..

internal void PageDeleted(Page page)
{
if (this.pages == null) { return; }
this.pages.InnerList.Remove(page.PageID);
}


I always use it because I like to be consistent in the format of my code and
it does immediately identify the scope. Else people end up doing that ugly
m_ stuff. So I prefer always instead of "when needed".

SP
Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.