All VB 2008 data types have a default value that will automatically be assigned to the variable. The default values are very predictable, and can be summarized as follows:
• Boolean variables are set to False.
• Numeric data is set to 0 (or 0.0 in the case of floating-point data types).
• Char variables are set to a single empty character.
• Uninitialized object references (including String types) are set to Nothing.
Given these rules, ponder the following code, which illustrates the default values assigned to members in a Module (the same defaults would be applied to members of a Class as well):
' Fields of a class or module receive automatic default assignments.
Public myInt As Integer ' Set to 0.
Public myString As String ' Set to Nothing.
Public myBool As Boolean ' Set to False.
Public myObj As Object ' Set to Nothing. End Module
In Visual Basic 2008, the same rules of default values hold true for local variables defined within a given scope. Given this, the following method would return the value 0, as each local Integer has been automatically assigned the value 0:
Function Add() As Integer Dim a, b As Integer Return a + b ' Returns zero. End Function
Was this article helpful?