what is the difference between using me.controlname and _controlname?
Printable View
what is the difference between using me.controlname and _controlname?
Nothing at all. Just like there is no difference between these two statements:
VB Code:
Dim MyPoint1 As System.Drawing.Point Dim MyPoint2 As Point ' Assuming you have System.Drawing imported
The fully qualified name is usually better because it makes things easier to read and understand. It is good coding practice to include it.
The underscore is used to indicate that a variable is a class member as opposed to a local variable. Personally, I generally don't use it for the same reasons that I don't use Hungarian notation. It just isn't needed in the modern IDE. The one time I do use it is if I am exposing a variable through a property. It allows you to give the variable and property basically the same name, e.g.:Using Me also identifies a variable as being a member but that's not why I use it. I use it to get Intellisense. In the example above, if I didn't use Me I would have had to type "_someVar". Because I used Me I would only have to type "me._" and Intellsense would show me the variable. The Intellisense in C# 2005 is smart enough to give you help from the first letter you type, making use of "this" unnecessary, but all other versions (VB and C#) don't.VB Code:
Private _someVar As String Public Property SomeVar() As String Get Return Me._someVar End Get Set Me._someVar = Value End Set End Property
Ctrl-Space invokes Intellisense immediately. If you've typed some chars already, it uses what's typed as a starting point.... if you haven't, then you get the intellisense with everything in it.
-tg
Now that I didn't know. You is da gnome dat knows. :)Quote:
Originally Posted by techgnome
Not only that but it works in VB6 VS2002/2003/2005 .... and probably earlier versions of VS too.
-tg
Isn't is easier to type "me." and then select the variable name you are looking for that type it's name? :) I always use me.variableName, although the code looks annoying sometimes because of this...
I thought I might mention that Ctrl+J will give you a list of all possible variables/properties/methods/etc no matter where your cursor is in the code. Ctrl+Space does the same thing, except it also attempts to make a prediction based on what you already have typed in, and will fill it in for you if it finds only one possible match.Quote:
Originally Posted by jmcilhinney
Also, Ctrl+I will give you infromation about the current variable. Such as wether its data type.
Now that I didn't know. You is da mon-kay dat knows.
-tg
Seems to me that it might be beneficial for me to make a list of keyboard shortcuts. But, unlike my VB6 to .NET equivalent chart, the shortcuts are probably possible to find online.Quote:
Originally Posted by techgnome
W.R.T. the first post, Me.controlname and _controlname are two different variables so there's a lot of difference in fact :)
As John said, the underscore prefix is just a notation, used to indicate a private member variable.
You don't want to get too carried away with underscores as in many contexts anything more than a single underscore prefix denotes an indentifier as being reserved for the compiler.