Results 1 to 8 of 8

Thread: Should I add "Me" to my controls

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    Should I add "Me" to my controls

    I have a VB.Net program where I'm manipulating many controls with code. In much of my code I do not preface the controls with "Me". Example:

    Code:
    PreviousButton.BackColor = Color.Khaki
    versus

    Code:
    Me.PreviousButton.BackColor = Color.Khaki
    Is it worth my while to go through the program and add Me. to the controls? Or should I leave it the way it is?

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: Should I add "Me" to my controls

    For the most part, it's largely a matter of personal preference and style. I sometimes do it for clarity, or where there is ambiguity.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Should I add "Me" to my controls

    Yes, it's all philosophical.

    Some people think code is more clear if you always do it. Other people think code is more clear if you never do it.

    My preference is to avoid using it unless I absolutely have to. When I find myself in a situation where I have to use it, I ask myself if I should consider other designs. Most of the time, the answer is "nah, this is harmless".

    This happens more in VB .NET than C#, mostly due to VB not being case-sensitive. Most people use conventions that sort of get around it.

    Do whatever brings your heart joy, in this case. Even when I'm specifically reviewing someone else's code, I rarely bring it up. There are worse practices than overusing Me.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Should I add "Me" to my controls

    I tend to add Me simply because it is quick to type, and after the ".", then intellisense will get you close to the right item. That won't happen if you just start typing. However, I believe that in VS2017, you will be encouraged to drop the Me.

    Aside from that, it makes no difference. There is no performance implication one way or the other.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    Re: Should I add "Me" to my controls

    But say you have 2 forms (with separate code and designers) and both of these have a PreviousButton on them. and they are opened at the same time then it would be safer to use Me. I don't have that problem in my current project but it would seem to me it would be safer to Me the controls.

  6. #6
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: Should I add "Me" to my controls

    Quote Originally Posted by projecttoday View Post
    But say you have 2 forms (with separate code and designers) and both of these have a PreviousButton on them. and they are opened at the same time then it would be safer to use Me. I don't have that problem in my current project but it would seem to me it would be safer to Me the controls.
    That wouldn't make any difference, if you don't specify Me it is assumed anyway. Two different forms would not have a problem here, the only time I have ever seen Me being needed is when a variable in a method shares a name with a variable at the class level, using Me allows you to specify the class level variable; then again I would personally have issues with this kind of naming scheme anyway...

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Should I add "Me" to my controls

    Me just means "this hear instance." Consider that you can have multiple instances of the SAME form being shown. All those instances will certainly all have the same buttons, so how does the code know which button was clicked? The button is part of a particular instance of the form, and that's all that Me really means. Every class has that, just so it knows which instance of the class is being talked about.
    My usual boring signature: Nothing

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Should I add "Me" to my controls

    I would suggest that the most important thing is to be consistent in whatever you do. Inconsistency can lead to confusion, because if someone reading your code (including you, some time after writing it) may well think that two pieces of code do different things if they are written it two different ways.

    I used to use Me in VB and 'this' in C# all the time. I now don't use them at all unless it's required. The most common situation where it's needed is when you have a constructor parameter that is used to set a field/property of the same name, e.g.
    vb.net Code:
    1. Public Property Name As String
    2.  
    3. Public Sub New(name As String)
    4.     Me.Name = name
    5. End Sub
    In such cases, I would say that it is desirable to use the same name for the parameter as the property, because it makes it plain to the caller what the value represents. As suggested, the disambiguation is not required in C# but I would still tend to use 'this' there too, because it's too easy to either miss that identifiers are different only by the case of their first letter, or it's too easy to use the wrong one in code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width