473,769 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.P age? For example, when you use the Session object, do you
say "this.Session.p roperty" or do you just say "Session.proper ty", and leave
off "this"?

I'm tending towards always using "this.Sessi on" and "this.Respo nse" and
"this.Label5.Te xt" - 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 5493
Use whatever you feel is clearer. They are both the same.

"Thomas H" <T@H> wrote in message
news:%2******** ********@tk2msf tngp13.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.P age? For example, when you use the Session object, do you
say "this.Session.p roperty" or do you just say "Session.proper ty", and
leave off "this"?

I'm tending towards always using "this.Sessi on" and "this.Respo nse" and
"this.Label5.Te xt" - 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******** ********@tk2msf tngp13.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.P age? For example, when you use the Session object, do you
say "this.Session.p roperty" or do you just say "Session.proper ty", and
leave off "this"?

I'm tending towards always using "this.Sessi on" and "this.Respo nse" and
"this.Label5.Te xt" - 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******** ********@tk2msf tngp13.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.P age? For example, when you use the Session object, do you
say "this.Session.p roperty" or do you just say "Session.proper ty", and leave off "this"?

I'm tending towards always using "this.Sessi on" and "this.Respo nse" and
"this.Label5.Te xt" - 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******** ********@tk2msf tngp13.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.P age? For example, when you use the Session object, do you
say "this.Session.p roperty" or do you just say "Session.proper ty", and
leave off "this"?

I'm tending towards always using "this.Sessi on" and "this.Respo nse" and
"this.Label5.Te xt" - 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.P age? For example, when you use the Session object, do
you say "this.Session.p roperty" or do you just say
"Session.proper ty", 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.d e
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******** ********@tk2msf tngp13.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.P age? For example, when you use the Session object, do you
say "this.Session.p roperty" or do you just say "Session.proper ty", and
leave off "this"?

I'm tending towards always using "this.Sessi on" and "this.Respo nse" and
"this.Label5.Te xt" - 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
1579
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 some don't like). Your oppinion please? ... internal void PageDeleted(Page page) {
6
2715
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 inherit from a QuickFix.Application class. I have this object: private static SocketInitiator qfxInitiator; When instantiated, its constructor need a "QuickFix.Application" object.
5
2476
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); When attempting to compile this code, a message is returned that states that the "this" keyword is not available in this context.
7
2249
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 me. So, I'm here for an answer (more of a confirmation), hopefully. First let me say that I know people keep saying; it doesn't work because the member "is a private". I believe there's more to it than just simply that... Theory: You inherit,...
1
2180
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
1195
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
2236
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"); this.textbox.type = "text"; this.textbox.addEventListener("blur", this.doThings, false);
4
1475
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
1498
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
9579
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9422
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9857
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8867
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7404
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5294
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3952
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2812
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.