To "this." or not to "this.", that is the question...
-
Hi there I'm relatively new to C#, so please exuse me if this seems n00bish... But, when referencing a control on a form is it good practice to include "this.", for example:
**textBox1.Text = "Hello World";**
-or-**this.textBox1.Text = "Hello World";**
I ask this because I haven't seen any convention mentioned anywhere, or consistency. Does it even matter? Also, I note that within a contructor for a class, you might use the same variable name for a parsed variable as you would for the private local variable, eg:public class fclsHelloWorld: Form { private bool sayHelloWorld; public fclsHelloWorld ( bool args, bool sayHelloWorld ) { InitializeComponent (); this.sayHelloWorld = sayHelloWorld; } ... }
So, it's obviously needed to specify which variable you're referring to. Thanks in advance :-D Cheers Poolee
... pessimists are rarely disappointed ...
-
Hi there I'm relatively new to C#, so please exuse me if this seems n00bish... But, when referencing a control on a form is it good practice to include "this.", for example:
**textBox1.Text = "Hello World";**
-or-**this.textBox1.Text = "Hello World";**
I ask this because I haven't seen any convention mentioned anywhere, or consistency. Does it even matter? Also, I note that within a contructor for a class, you might use the same variable name for a parsed variable as you would for the private local variable, eg:public class fclsHelloWorld: Form { private bool sayHelloWorld; public fclsHelloWorld ( bool args, bool sayHelloWorld ) { InitializeComponent (); this.sayHelloWorld = sayHelloWorld; } ... }
So, it's obviously needed to specify which variable you're referring to. Thanks in advance :-D Cheers Poolee
... pessimists are rarely disappointed ...
I almost never use
this
, unless it is the constructor case you mentioned.Luis Alonso Ramos Intelectix Chihuahua, Mexico
-
Hi there I'm relatively new to C#, so please exuse me if this seems n00bish... But, when referencing a control on a form is it good practice to include "this.", for example:
**textBox1.Text = "Hello World";**
-or-**this.textBox1.Text = "Hello World";**
I ask this because I haven't seen any convention mentioned anywhere, or consistency. Does it even matter? Also, I note that within a contructor for a class, you might use the same variable name for a parsed variable as you would for the private local variable, eg:public class fclsHelloWorld: Form { private bool sayHelloWorld; public fclsHelloWorld ( bool args, bool sayHelloWorld ) { InitializeComponent (); this.sayHelloWorld = sayHelloWorld; } ... }
So, it's obviously needed to specify which variable you're referring to. Thanks in advance :-D Cheers Poolee
... pessimists are rarely disappointed ...
In most cases, I've just seen people use the "this" keyword to trigger intellisense.
Deja View - the feeling that you've seen this post before.
-
Hi there I'm relatively new to C#, so please exuse me if this seems n00bish... But, when referencing a control on a form is it good practice to include "this.", for example:
**textBox1.Text = "Hello World";**
-or-**this.textBox1.Text = "Hello World";**
I ask this because I haven't seen any convention mentioned anywhere, or consistency. Does it even matter? Also, I note that within a contructor for a class, you might use the same variable name for a parsed variable as you would for the private local variable, eg:public class fclsHelloWorld: Form { private bool sayHelloWorld; public fclsHelloWorld ( bool args, bool sayHelloWorld ) { InitializeComponent (); this.sayHelloWorld = sayHelloWorld; } ... }
So, it's obviously needed to specify which variable you're referring to. Thanks in advance :-D Cheers Poolee
... pessimists are rarely disappointed ...
I've seen people use it for intellisense (however, CTRL+J will bring up intellisense without having to type 'this'). Some tools, such as widely-used Resharper tool issues warnings for using 'this' when it's not needed. Since it purely is excess typing and more words to read while reading code, I recommend against using 'this' keyword unless it's warranted (e.g. the constructor example or passing the current class instance into a function).
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Minnesota Bridge Collapses The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
Hi there I'm relatively new to C#, so please exuse me if this seems n00bish... But, when referencing a control on a form is it good practice to include "this.", for example:
**textBox1.Text = "Hello World";**
-or-**this.textBox1.Text = "Hello World";**
I ask this because I haven't seen any convention mentioned anywhere, or consistency. Does it even matter? Also, I note that within a contructor for a class, you might use the same variable name for a parsed variable as you would for the private local variable, eg:public class fclsHelloWorld: Form { private bool sayHelloWorld; public fclsHelloWorld ( bool args, bool sayHelloWorld ) { InitializeComponent (); this.sayHelloWorld = sayHelloWorld; } ... }
So, it's obviously needed to specify which variable you're referring to. Thanks in advance :-D Cheers Poolee
... pessimists are rarely disappointed ...
-
Hi there I'm relatively new to C#, so please exuse me if this seems n00bish... But, when referencing a control on a form is it good practice to include "this.", for example:
**textBox1.Text = "Hello World";**
-or-**this.textBox1.Text = "Hello World";**
I ask this because I haven't seen any convention mentioned anywhere, or consistency. Does it even matter? Also, I note that within a contructor for a class, you might use the same variable name for a parsed variable as you would for the private local variable, eg:public class fclsHelloWorld: Form { private bool sayHelloWorld; public fclsHelloWorld ( bool args, bool sayHelloWorld ) { InitializeComponent (); this.sayHelloWorld = sayHelloWorld; } ... }
So, it's obviously needed to specify which variable you're referring to. Thanks in advance :-D Cheers Poolee
... pessimists are rarely disappointed ...
I think it is a matter of style. I don't have anything against using
this
when it is superflous and I won't go around changing anyone's code that uses it. My personal style is to not usethis
unless it is needed or unless it helps code readability.
Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." My website
-
Hi there I'm relatively new to C#, so please exuse me if this seems n00bish... But, when referencing a control on a form is it good practice to include "this.", for example:
**textBox1.Text = "Hello World";**
-or-**this.textBox1.Text = "Hello World";**
I ask this because I haven't seen any convention mentioned anywhere, or consistency. Does it even matter? Also, I note that within a contructor for a class, you might use the same variable name for a parsed variable as you would for the private local variable, eg:public class fclsHelloWorld: Form { private bool sayHelloWorld; public fclsHelloWorld ( bool args, bool sayHelloWorld ) { InitializeComponent (); this.sayHelloWorld = sayHelloWorld; } ... }
So, it's obviously needed to specify which variable you're referring to. Thanks in advance :-D Cheers Poolee
... pessimists are rarely disappointed ...
You specify
this
to tell the compiler or yourself it's about this class, not somewhere else. I almost only use it in the constructor example you mentioned, and very occasionally when eg I do something with a form likethis.Close();
orif(this.DialogResult == DialogResult.OK)...
One of my colleagues usesthis
to point out variables that are defined globally in the class. Please, for the sake of your dear fellow colleagues, avoidthis
..... in this case ;p !V. No hurries, no worries
-
Hi there I'm relatively new to C#, so please exuse me if this seems n00bish... But, when referencing a control on a form is it good practice to include "this.", for example:
**textBox1.Text = "Hello World";**
-or-**this.textBox1.Text = "Hello World";**
I ask this because I haven't seen any convention mentioned anywhere, or consistency. Does it even matter? Also, I note that within a contructor for a class, you might use the same variable name for a parsed variable as you would for the private local variable, eg:public class fclsHelloWorld: Form { private bool sayHelloWorld; public fclsHelloWorld ( bool args, bool sayHelloWorld ) { InitializeComponent (); this.sayHelloWorld = sayHelloWorld; } ... }
So, it's obviously needed to specify which variable you're referring to. Thanks in advance :-D Cheers Poolee
... pessimists are rarely disappointed ...
Indexers have to be declared with 'this'.
public string this[string theThingIWant ] { get { return _myListOfStuff[theThingIWant]; } set { myListOfStuff[theThingIWant] = value; } }
-- modified at 3:13 Friday 3rd August, 2007
"More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF
-
You specify
this
to tell the compiler or yourself it's about this class, not somewhere else. I almost only use it in the constructor example you mentioned, and very occasionally when eg I do something with a form likethis.Close();
orif(this.DialogResult == DialogResult.OK)...
One of my colleagues usesthis
to point out variables that are defined globally in the class. Please, for the sake of your dear fellow colleagues, avoidthis
..... in this case ;p !V. No hurries, no worries
V. wrote:
One of my colleagues uses this to point out variables that are defined globally in the class. Please, for the sake of your dear fellow colleagues, avoid this ..... in this case
I find it more readable when this is used like this :P It's all down to style and personal preference.
-
V. wrote:
One of my colleagues uses this to point out variables that are defined globally in the class. Please, for the sake of your dear fellow colleagues, avoid this ..... in this case
I find it more readable when this is used like this :P It's all down to style and personal preference.
yeah man f*ck
this
:laugh:V.
Stop smoking so you can: Enjoy longer the money you save. Moviereview Archive -
Hello,
Poolee wrote:
this.sayHelloWorld = sayHelloWorld;
X| I wouldn't want to see a codeline like this in my project! There for, I only need "this" when passing an instance to a function. (like Judah pointed out very well)
All the best, Martin
-
Thanks for your reply. So just to clarify, you would use a differently-named private variable to the passed variable name? Cheers Poolee
... pessimists are rarely disappointed ...