Alex Rivera | Logout

Naming convention for VB.NET private fields

Asked 2008-08-11T09:41:11.373
24

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?

Edit
Report

3 Answers

21

I still use the _ prefix in VB for private fields, so I'll have _foo as the private field and Foo as the property. I do this for c# as well and pretty much any code I write. Generally I wouldn't get too caught up in "what is the right way to do it" because there isn't really a "right" way (altho there are some very bad ways) but rather be concerned with doing it consistently.

At the end of the day, being consistent will make your code much more readable and maintainable than using any set of "right" conventions.

answered 2008-08-11T09:45:27.967
3

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.

answered 2011-08-29T20:55:31.463
0

I agree with @lomaxx, it's more important to be consistent throughout the team than to have the right convention.

Still, here are several good places to get ideas and guidance for coding conventions:

  1. Practical Guidelines and Best Practices for Microsoft Visual Basic and Visual C# Developers by Francesco Balena is a great book that addresses many of these issues.
  2. IDesign Coding Standards (for C# and for WCF)
  3. The .NET Framework Source Code (in VS2008)
answered 2008-08-11T13:05:11.790

Your Answer