-
Feb 23rd, 2018, 10:08 AM
#1
Thread Starter
Fanatic Member
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?
-
Feb 23rd, 2018, 10:18 AM
#2
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
-
Feb 23rd, 2018, 11:16 AM
#3
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.
-
Feb 23rd, 2018, 11:56 AM
#4
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
-
Feb 23rd, 2018, 12:17 PM
#5
Thread Starter
Fanatic Member
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.
-
Feb 23rd, 2018, 12:43 PM
#6
Re: Should I add "Me" to my controls
Originally Posted by projecttoday
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...
-
Feb 23rd, 2018, 04:55 PM
#7
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
-
Feb 23rd, 2018, 07:49 PM
#8
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:
Public Property Name As String Public Sub New(name As String) Me.Name = name 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|