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

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

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.