Naming convention for VB.NET private fields

Question!

Is there an official convention for naming private fields in VB.NET? For example, if I have a property called 'Foo', I normally call the private field '_Foo'. This seems to be frowned upon in the Offical Guidelines:

"Do not use a prefix for field names. For example, do not use g_ or s_ to distinguish static versus non-static fields."

In C#, you could call the private field 'foo', the property 'Foo', and refer to the private field as 'this.foo' in the constructor. As VB.NET is case insensitive you can't do this - any suggestions?



Answers

In VB.NET 4.0, most of you probably know you don't need to explicitly write getters and setters for your Property declarations as follows:

Public Property Foo As String
Public Property Foo2 As String

VB automatically creates private member variables called _Foo and _Foo2. It seems as though Microsoft and the VS team have adopted the _ convention, so I don't see an issue with it.

By : crunchy


It's personal preference, although there's widespread support for having some distinction. Even in C# I don't think there's one widely used convention.

Jeff Prosise says

As a matter of personal preference I typically prefix private fields with an underscore [in C#] ... This convention is used quite a lot in the .NET framework but it is not used throughout.

From the .NET Framework Design Guidelines 2nd Edition page 73.

Jeffrey Richter says

I make all my fields private and I prefix my instance fields with "m_" and my static fields with "s_" [in C#]

From the .NET Framework Design Guidelines 2nd Edition page 47. Anthony Moore (BCL team) also thinks using "m_" and "s_" is worth consideration, page 48.

By : MarkJ


I agree most important is not what style one uses but it being consistent.

With that said, the new MS/.NET styling for private fields tends to be _fooVar (underscore followed by a camelCased name)

By : sergeb


This video can help you solving your question :)
By: admin