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

T

Thomas H

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
 
K

Kevin Spencer

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.
 
K

Karl Seguin

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
 
S

Scott M.

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.
 
J

Joerg Jooss

Thomas said:
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,
 
T

Thomas H

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top