Coding Conventions - Visual Basic (original) (raw)

Microsoft develops samples and documentation that follow the guidelines in this topic. If you follow the same coding conventions, you may gain the following benefits:

Naming Conventions

Layout Conventions

a As Integer,  
b As Integer  
' Here is a comment.  

Program Structure

Sub Main()  
  For Each argument As String In My.Application.CommandLineArgs  
    ' Add code here to use the string variable.  
  Next  
End Sub  

Language Guidelines

String Data Type

MsgBox($"hello{vbCrLf}goodbye")  
Dim longString As New System.Text.StringBuilder  
For count As Integer = 1 To 1000  
  longString.Append(count)  
Next  

Relaxed Delegates in Event Handlers

Do not explicitly qualify the arguments (Object and EventArgs) to event handlers. If you are not using the event arguments that are passed to an event (for example, sender As Object, e As EventArgs), use relaxed delegates, and leave out the event arguments in your code:

Public Sub Form1_Load() Handles Form1.Load
End Sub

Unsigned Data Type

Arrays

Dim letters1 As String() = {"a", "b", "c"}  

Do not use the following syntax.

Dim letters2() As String = New String() {"a", "b", "c"}  
Dim letters4 As String() = {"a", "b", "c"}  

Do not use the following syntax:

Dim letters3() As String = {"a", "b", "c"}  
Dim letters5 As String() = {"a", "b", "c"}  

Do not use the following syntax:

Dim letters6(2) As String  
letters6(0) = "a"  
letters6(1) = "b"  
letters6(2) = "c"  

Use the With Keyword

When you make a series of calls to one object, consider using the With keyword:

With orderLog
  .Log = "Application"
  .Source = "Application Name"
  .MachineName = "Computer Name"
End With

Use the Try...Catch and Using Statements when you use Exception Handling

Do not use On Error Goto.

Use the IsNot Keyword

Use ... IsNot Nothing instead of Not ... Is Nothing.

New Keyword

Dim employees As New List(Of String)  

The preceding line is equivalent to this:

Dim employees2 As List(Of String) = New List(Of String)  
Dim orderLog As New EventLog With {  
    .Log = "Application",  
    .Source = "Application Name",  
    .MachineName = "Computer Name"}  

Event Handling

Private Sub ToolStripMenuItem1_Click() Handles ToolStripMenuItem1.Click  
End Sub  
Dim closeItem As New ToolStripMenuItem(  
    "Close", Nothing, AddressOf ToolStripMenuItem1_Click)  
Me.MainMenuStrip.Items.Add(closeItem)  
Public Event SampleEvent As EventHandler(Of SampleEventArgs)  
' or  
Public Event SampleEvent(ByVal source As Object,  
                          ByVal e As SampleEventArgs)  

Using Shared Members

Call Shared members by using the class name, not from an instance variable.

Use XML Literals

XML literals simplify the most common tasks that you encounter when you work with XML (for example, load, query, and transform). When you develop with XML, follow these guidelines:

Private Function GetHtmlDocument(  
    ByVal items As IEnumerable(Of XElement)) As String  
  Dim htmlDoc = <html>  
                  <body>  
                    <table border="0" cellspacing="2">  
                      <%=  
                        From item In items  
                        Select <tr>  
                                 <td style="width:480">  
                                   <%= item.<title>.Value %>  
                                 </td>  
                                 <td><%= item.<pubDate>.Value %></td>  
                               </tr>  
                      %>  
                    </table>  
                  </body>  
                </html>  
  Return htmlDoc.ToString()  
End Function  

LINQ Queries

Dim seattleCustomers = From cust In customers  
                       Where cust.City = "Seattle"  
Dim customerOrders = From customer In customers  
                     Join order In orders  
                       On customer.CustomerID Equals order.CustomerID  
                     Select Customer = customer, Order = order  
Dim customerOrders2 = From cust In customers  
                      Join ord In orders  
                        On cust.CustomerID Equals ord.CustomerID  
                      Select CustomerName = cust.Name,  
                             OrderID = ord.ID  
Dim customerList = From cust In customers  
Dim newyorkCustomers = From cust In customers  
                       Where cust.City = "New York"  
                       Select cust.LastName, cust.CompanyName  
Dim newyorkCustomers2 = From cust In customers  
                        Where cust.City = "New York"  
                        Order By cust.LastName  
Dim customerList2 = From cust In customers  
                    Join order In orders  
                      On cust.CustomerID Equals order.CustomerID  
                    Select cust, order  

See also