473,387 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 5470
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Anders Borum | last post by:
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...
6
by: Marty | last post by:
Hi, I have a class that I modified to be static. It is now a public sealed class and all function are static, no more constructor but a init() function to do the constructor job. This class...
5
by: ChrisB | last post by:
Hello: An object that is a field in another object has a constructor that requires a reference to the containing object: // object fields ChildObject childObject = new ChildObject(this); ...
7
by: relient | last post by:
Question: Why can't you access a private inherited field from a base class in a derived class? I have a *theory* of how this works, of which, I'm not completely sure of but makes logical sense to...
1
by: goe | last post by:
Hi, Im my function I need to use focus() and select() based on the "this" keyword. example: onchange="validate(this)" function validate(input) {
3
by: 2b|!2b==? | last post by:
Is this legal ? MyClass& MyClass::Clone() const { //this should invoke the copy ctor ... //but can I reference an object within itself ? MyClass* mc = new MyClass(*this); return *mc ; };
5
by: Jeremy | last post by:
Is there any good reading about pitfalls of scoping when using events? Here is my specific issue: function MyType() { this.foo = "bar"; this.textbox = document.createElement("input");...
4
by: pbd22 | last post by:
Hi. I am wondering what do I replace the "this" statement with to get the appropriate container/response. When I try: cal.prototype.dates(cal.days.day); I get
2
by: ngkrishnan | last post by:
Hello i am new to the java technology..... will u plz help in understanding the concept of ..."not using the THIS keyword in the STATIC methods..........." regards ngk
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.