.NET Naming Conventions – VB.NET version




Not so long ago a colleague of mine, Josh Twist, did a post on .NET naming conventions using C# code to serve as an example. These are based on the .NET naming guidelines published on MSDN. Well, I happen to be doing a code review right now, but it’s in VB.NET. So I thought I’d do a VB.NET version and post it here for everyone’s reference. I’m sure that Eric Nelson will be very happy!

Option Explicit On
Option Strict On

Imports System

‘ Namespaces are PascalCased
Namespace Example.NamingConventions

‘ Class names are PascalCased
Public Class ExampleClass

‘ All public fields, including constants are PascalCased
Public Const PiAsAString As String = “3.14”

‘ All private fields are camelCased with an underscore prefix
Private ReadOnly _privateMember As String

‘ All protected members are PascalCased
Protected ProtectedField As Integer = 12

‘ All friend members are PascalCased
Friend InternalField As Integer = 13

‘ All private methods are PascalCased
‘ *** NOTE – All parameters are camelCased
Private Function Multiply(ByVal valueA As Double, ByVal valueB As Double) As Double
‘ local variables (scoped within a method) are camelCased (no underscore)
Dim result As Double = valueA * valueB
Return result
End Function

‘ All private Properties are PascalCased
‘ *** NOTE – Acronyms of 2 characters are UPPERCASED (e.g. UI, IO)
Private ReadOnly Property UIElementName() As String
Get
Throw New
NotImplementedException()
End Get
End Property

‘ All (public and private) properties are PascalCased
‘ *** NOTE – Acronyms longer than 2 characters are PascalCased (e.g. Html, Xml)
Public Property HtmlLength() As Integer
Get
Throw New
NotImplementedException()
End Get
Set
(ByVal value As Integer)
Throw New NotImplementedException()
End Set
End Property

‘ All public methods are PascalCased
‘ *** NOTE – All parameters are camelCased
‘ *** NOTE – Abbreviations are not treated as Acronyms (so “Id” is Id, not ID).
Public Sub AlignObjectById(ByVal id As String, ByVal alignment As Alignment)
Throw New NotImplementedException()
End Sub

‘ Nested classes are PascalCased, even private ones
Private Class NestedClass
Implements IDisposable

Public Sub Dispose() Implements IDisposable.Dispose
Throw New NotImplementedException()
End Sub

End Class
End Class

‘ Enums are PascalCased and not plural (unless marked with <Flags> in which case the name should be plural)
Public Enum Alignment
‘ Enum members are PascalCased
Top
Bottom
Left
Right
End Enum

End Namespace


Originally posted by Rupert Benbrook on 30 January 2010 here.

Comments (0)