Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. To "this." or not to "this.", that is the question...

To "this." or not to "this.", that is the question...

Scheduled Pinned Locked Moved C#
questioncsharptutorial
12 Posts 9 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Poolee
    wrote on last edited by
    #1

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

    L P J M C 7 Replies Last reply
    0
    • P Poolee

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

      L Offline
      L Offline
      Luis Alonso Ramos
      wrote on last edited by
      #2

      I almost never use this, unless it is the constructor case you mentioned.

      Luis Alonso Ramos Intelectix Chihuahua, Mexico

      My Blog!

      1 Reply Last reply
      0
      • P Poolee

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

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • P Poolee

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

          J Offline
          J Offline
          Judah Gabriel Himango
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • P Poolee

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

            M Offline
            M Offline
            Martin 0
            wrote on last edited by
            #5

            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

            P 1 Reply Last reply
            0
            • P Poolee

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

              C Offline
              C Offline
              Colin Angus Mackay
              wrote on last edited by
              #6

              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 use this 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

              1 Reply Last reply
              0
              • P Poolee

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

                V Offline
                V Offline
                V 0
                wrote on last edited by
                #7

                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 like this.Close(); or if(this.DialogResult == DialogResult.OK)... 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 ;p !

                V. No hurries, no worries

                O 1 Reply Last reply
                0
                • P Poolee

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

                  M Offline
                  M Offline
                  Malcolm Smart
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • V V 0

                    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 like this.Close(); or if(this.DialogResult == DialogResult.OK)... 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 ;p !

                    V. No hurries, no worries

                    O Offline
                    O Offline
                    originSH
                    wrote on last edited by
                    #9

                    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 1 Reply Last reply
                    0
                    • O originSH

                      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 Offline
                      V Offline
                      V 0
                      wrote on last edited by
                      #10

                      yeah man f*ck this :laugh:

                      V.
                      Stop smoking so you can: Enjoy longer the money you save. Moviereview Archive

                      1 Reply Last reply
                      0
                      • M Martin 0

                        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

                        P Offline
                        P Offline
                        Poolee
                        wrote on last edited by
                        #11

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

                        M 1 Reply Last reply
                        0
                        • P Poolee

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

                          M Offline
                          M Offline
                          Martin 0
                          wrote on last edited by
                          #12

                          Poolee wrote:

                          So just to clarify, you would use a differently-named private variable to the passed variable name?

                          Yes!!!!

                          All the best, Martin

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups