469,600 Members | 2,185 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,600 developers. It's quick & easy.

Opinion of using "this" keyword on C# ASP.NET pages?

Hi everyone, I've got a question that's more of a "style" question...

Do you guys reference "this" for every object that's inherited from
System.Web.UI.Page? For example, when you use the Session object, do you
say "this.Session.property" or do you just say "Session.property", and leave
off "this"?

I'm tending towards always using "this.Session" and "this.Response" and
"this.Label5.Text" - even though I don't have to. I have the feeling that
I'm over-using it.

Just wondering if it's proper style to always use "this"... or if "this"
should only be used when you have the different objects with the same name
in different scopes (i.e. you have a public strMyString but want to
reference the local private strMyString).

Thanks!

-Thomas H
Nov 19 '05 #1
6 5315
Use whatever you feel is clearer. They are both the same.

"Thomas H" <T@H> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi everyone, I've got a question that's more of a "style" question...

Do you guys reference "this" for every object that's inherited from
System.Web.UI.Page? For example, when you use the Session object, do you
say "this.Session.property" or do you just say "Session.property", and
leave off "this"?

I'm tending towards always using "this.Session" and "this.Response" and
"this.Label5.Text" - even though I don't have to. I have the feeling that
I'm over-using it.

Just wondering if it's proper style to always use "this"... or if "this"
should only be used when you have the different objects with the same name
in different scopes (i.e. you have a public strMyString but want to
reference the local private strMyString).

Thanks!

-Thomas H

Nov 19 '05 #2
Hi Thomas,

"this" is generally unnecessary, as the compiler will crunch it all down
anyway. However, there are situations in which it is quite useful:

1. Passing "this" as a parameter - Can't be done without it.
2. Readability - If your code gets very complex, sometimes "this" can
provide a clue for the developer.
3. Ambiguous name reference.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.

"Thomas H" <T@H> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi everyone, I've got a question that's more of a "style" question...

Do you guys reference "this" for every object that's inherited from
System.Web.UI.Page? For example, when you use the Session object, do you
say "this.Session.property" or do you just say "Session.property", and
leave off "this"?

I'm tending towards always using "this.Session" and "this.Response" and
"this.Label5.Text" - even though I don't have to. I have the feeling that
I'm over-using it.

Just wondering if it's proper style to always use "this"... or if "this"
should only be used when you have the different objects with the same name
in different scopes (i.e. you have a public strMyString but want to
reference the local private strMyString).

Thanks!

-Thomas H

Nov 19 '05 #3
I agree with Peter though I tend to exclude the "this". Inheritance
represents an "is a" relationship. As in the class for addUser.aspx IS A
Page. as such I consider whatever goodies Page exposes are mine and require
no different distinction from my own local variables..... I don't see what
benefit this.Session provides....

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Thomas H" <T@H> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi everyone, I've got a question that's more of a "style" question...

Do you guys reference "this" for every object that's inherited from
System.Web.UI.Page? For example, when you use the Session object, do you
say "this.Session.property" or do you just say "Session.property", and leave off "this"?

I'm tending towards always using "this.Session" and "this.Response" and
"this.Label5.Text" - even though I don't have to. I have the feeling that
I'm over-using it.

Just wondering if it's proper style to always use "this"... or if "this"
should only be used when you have the different objects with the same name
in different scopes (i.e. you have a public strMyString but want to
reference the local private strMyString).

Thanks!

-Thomas H

Nov 19 '05 #4
The same question comes up in VB.NET, although the keyword is "Me", not
"this". What some people like is that if you use "Me" in VB.NET and then
type the dot (.), you'll get intelliSense and this list will contain all the
control names you've created for the page. This comes in handy when you
can't remember exactly what all your controls are named.

Ultimately, it doesn't matter. It's whatever is best for you.
"Thomas H" <T@H> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi everyone, I've got a question that's more of a "style" question...

Do you guys reference "this" for every object that's inherited from
System.Web.UI.Page? For example, when you use the Session object, do you
say "this.Session.property" or do you just say "Session.property", and
leave off "this"?

I'm tending towards always using "this.Session" and "this.Response" and
"this.Label5.Text" - even though I don't have to. I have the feeling that
I'm over-using it.

Just wondering if it's proper style to always use "this"... or if "this"
should only be used when you have the different objects with the same name
in different scopes (i.e. you have a public strMyString but want to
reference the local private strMyString).

Thanks!

-Thomas H

Nov 19 '05 #5
Thomas H wrote:
Hi everyone, I've got a question that's more of a "style" question...

Do you guys reference "this" for every object that's inherited from
System.Web.UI.Page? For example, when you use the Session object, do
you say "this.Session.property" or do you just say
"Session.property", and leave off "this"?


I use it only for accessing (private) instance fields, but not methods
or properties.
Cheers,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Nov 19 '05 #6
Everyone, thanks for the responses! If everyone leaves off "this" (unless
it's absolutely needed), I'll do the same. At the least, it'll save me 5
characters of typing!

-Thomas

"Thomas H" <T@H> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi everyone, I've got a question that's more of a "style" question...

Do you guys reference "this" for every object that's inherited from
System.Web.UI.Page? For example, when you use the Session object, do you
say "this.Session.property" or do you just say "Session.property", and
leave off "this"?

I'm tending towards always using "this.Session" and "this.Response" and
"this.Label5.Text" - even though I don't have to. I have the feeling that
I'm over-using it.

Just wondering if it's proper style to always use "this"... or if "this"
should only be used when you have the different objects with the same name
in different scopes (i.e. you have a public strMyString but want to
reference the local private strMyString).

Thanks!

-Thomas H

Nov 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Anders Borum | last post: by
6 posts views Thread by Marty | last post: by
5 posts views Thread by ChrisB | last post: by
7 posts views Thread by relient | last post: by
5 posts views Thread by Jeremy | last post: by
4 posts views Thread by pbd22 | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.