tags:

views:

1090

answers:

4

I've seen some variable declare in VB.net in several way like:

print("dim _Foo as string");

and print("dim m_Foo as string"); and print("dim foo as string");

I will like to know what's the standard for VB.net coding.

+2  A: 

It all depends on the scope. In the case of:

Private Dim m_Foo As String

That implies that m_Foo is a member of a class. This also implies the same thing:

Private Dim _Foo As String

It's a matter of preference.

On the other hand, something like this:

Dim Foo As String

might refer to a variable local to a given method. Some might even prefix it with a "l_":

Dim l_Foo As String

Declaring like these examples helps in determining scope when scanning code. Putting it all together, here's a sample class showing a well-known naming convention (but not the only one):

Public Class Bar
    Private m_firstName As String

    Public Sub New(ByVal firstName As String)
        m_firstName = firstName
    End Sub

    Public Function SayGreeting() As String
        Dim l_Greeting As String
        l_Greeting = String.Format("{0}, {1}!", "Hello", m_firstName)
        Return l_Greeting
    End Function
End Class
Bullines
+1  A: 

Using "m_Name" for member variables is standard in VB. I almost never meet someone who doesn't do it that way.

Many, but not all, use "s_Name" for Shared variables. This makes it clear how the variable is being used.

For local variables I use "name". Why? Well a parameter is essentially a local variable and it uses that pattern (as per .NET guidelines), so why not make all local variables look that way?

Jonathan Allen
why wouldn't parameters be p_Name?
Maslow
The naming of parameters is standardized in the .NET Framework Design Guidelines.
Jonathan Allen
A: 

I would disagree, I have seen many methds for prefixing class variables including "m", "m_" and _.

I have a completely different method that has served me well. I create a structure called backingStore which contains my class variables then I have one Private Variable called Local as new backingStore.

This allows me to have the same name for the property and the class variable and when referencing it, it is perfectly clear which one I am talking about. ex.

Local.FirstName = "Fred"

See complete sample here

Design Lunacy: Pain in the m_#$*^

A: 

It really depends on the naming conventions as agreed in your working environment. There are definitely guidelines available : Naming Conventions for .NET / C# Projects - Which is basically what we follow.

For example:

_member //Class member - Camel Case
intLocalVariable //note use of Hungarian notation - Camel Case
pMethodParameter //Camel Case
MyProperty //Pascal Case
Simon Hartcher