Skip to content

08. Visual Basic coding standards

Diane Dowling edited this page Feb 27, 2023 · 7 revisions

Reference: https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/program-structure/coding-conventions

Exemplar code

' A linear search algorithm that iterates through every item in the array
Function LinearSearch(ByRef items As Integer(), ByVal searchItem As Integer) As Integer
	' Declare and initialise a variable
	Dim foundIndex as Integer = -1

	' Repeat until the end of the array is reached
	For current = 0 To items.Length - 1
		' Compare the item at the current index to the search item
		If items(current) = searchItem Then
			' If the search item has been found, store the current index 
			foundIndex = current
		End If
	Next

	' Return the index of the searchItem or -1 if not found
	Return foundIndex
End Function

The exemplar code for Visual Basic in the GitHub version would also include a Main subroutine that set up some test data and called LinearSearch.

Naming conventions

  Guidance Example
Variables CamelCase totalPay
Constants Constants should be declared const and use capitals HOURLYRATE
Functions PascalCase CalculatePay
Parameters PascalCase HoursWorked
Class PascalCase (singular noun) GamePlayer
Methods PascalCase CalculatePay
Strings Must use double quotes "Hello world"
Characters Must use single quotes 'H'  

Spaces

  • No space between the function identifier and the parameter list
  • Single space either side of operators (unless used as a parameter)
  • Single space after commas in lists
  • Two lines between functions (or methods)
  • Use white space (single line) within a function if it helps readability

Comments

  • Comments start with a single quote (').
  • Insert one space between the comment delimiter (') and the comment text.
  • Start the comment text with an uppercase letter, and end comment text with a full stop.[CHECK THIS WITH TEAM]
  • Multiline comments: Start each line with a single quote
  • Use a single line comment above each function with important information about the function

Main method

  • VisualBasic applications always use the Main method as their entry point.
  • Main can be used for the whole program if the application is small and there is no need for additional subroutines.
Sub Main()
   Dim n as Integer = 6
   Dim result as Integer = SumToN(n)
   Console.WriteLine($"The sum of 1 to {n} is {result}")
End Dub

Declaring variables

Always use the Dim keyword as this is best practice.

Declaring an array

Use the syntax Dim LetterList As String() = {"a", "b", "c"}

String formatting

Example: Console.WriteLine($"The sum of 1 to {n} is {result}")

Comment for top of GitHub file [CHECK THIS SECTION]

The very first lines of code should be the default comment below which explains how to run the Visual Basic program. This comment should appear before the first programming statement or Imports statement.

' Raspberry Pi Foundation
' Developed as part of Ada Computer Science 
' Usage licensed under CC BY-NC-SA 4.0
' Note: This file is designed to be copied out and compiled on your machine.
' To run this file you need to:
' 1. Copy the contents
' 2. Paste them into the Visual Basic IDE of your choice (Visual Studio, for example)
' 3. Compile the program
' 4. Run the program

Clone this wiki locally