Meaning of _ underscore as a variable prefix in VB.net
Solution 1
It's a convention. A leading _ often indicates that the variable is private to the class. This convention is commonly used in many different languages, not just VB.
In a similar sense, it also indicates that the variable is the local variable behind a property.
However it has no significant meaning to the compiler.
Solution 2
FYI: if you are looking at VB code prior to the .NET era (ie: VB6, of which there is a ton out there) the _ character did have special meaning in that it was a line continuation character. Variables or lines could not begin with an _
Example of VB6 use of _:
Dim str As String
str = "This is part one of a very long string" & _
"Notice that this is more text" & _
"AND SOME MORE"
I am pretty sure that in VB.NET the _ continues to function as a line continuation character, however the variable name restriction has obviously been lifted.
Solution 3
At the end of a line, it can be used to split code across multiple lines if it's preceded by a space & the very next character is the new line (_ is the last symbol on the line & followed by a space.
see http://msdn.microsoft.com/en-us/library/ba9sxbw4.aspx
Solution 4
Many use the underscore prefix for field members of the class. These variables should be scoped as Private
. This is just a convention however.
Solution 5
The _ (Underscore symbol) is used just because to notify that it is a Private variable.
Related videos on Youtube
StealthRT
Updated on July 09, 2022Comments
-
StealthRT almost 2 years
What is the meaning of underscore in visual basic? I have this code:
Private _isAuthenticated As Boolean
Is that the same as doing this?
Private isAuthenticated As Boolean
Or does adding the underscore to the front of the name do something special?
-
StealthRT over 12 yearsSweet. Thanks for the knowledge, Phil! :)
-
StealthRT over 12 yearsSweet. Thanks for the knowledge, Peter! :)
-
codechurn over 12 yearsThe title of the post is 'Meaning of _ in VB.net', and quoting the question: "Or does adding the _ to the front of the name do something speical?" makes my comment about the fact that the _ can also serve as continuation character relevant and on topic.
-
Ozgur Ozturk over 8 yearsI guess many other people came here from google to find the line continuation usage, hence the upvotes. :) So John, it is relevant since google finds it relevant. :)
-
Bugs over 7 years@OzgurOzturk Later versions don't require the underscore to break up code however I guess some people do still use it. I'm not sure when it was phased out exactly (I think 2010). I use 2008 where I need the underscores and then 2013 where I don't. Can get a bit confusing when I haven't used 2008 in a while.
-
Vandrey over 4 yearsThank you! This was very helpful; especially the link helped a lot.