Cover | Table of Contents
Module Module1
Public Sub Main()
Dim x As Integer
x = 10
MsgBox(Increment(x))
End Sub
Private Function Increment(ByVal baseValue As Integer) As Integer
Return baseValue + 1
End Function
End ModuleModule is a variation of a class.Partial keyword. See the entry for that keyword in Chapter 12 for additional information on its usage.Public ClassclassName
End ClassNew keyword. These two steps are often performed in two separate VB
statements.Dim myInstance As SimpleClass ' Defines the variable myInstance = New SimpleClass ' Creates the object
Dim myInstance As SimpleClass = New SimpleClass
Dim myInstance As New SimpleClass
Field Members
Byte data type can support and manage any 8-bit unsigned integer value, from 0 to 255. It allows no other data values outside of this defined subset, but it handles this subset extremely well. .NET provides data types for those subsets of data that programmers have found essential in software development. These data types make it possible to manipulate virtually any variation of data. For those instances where a predefined .NET data type will not meet your needs, you can use the predefined data types
as building blocks to develop your own custom data management class.Byte data type can support and manage any 8-bit unsigned integer value, from 0 to 255. It allows no other data values outside of this defined subset, but it handles this subset extremely well. .NET provides data types for those subsets of data that programmers have found essential in software development. These data types make it possible to manipulate virtually any variation of data. For those instances where a predefined .NET data type will not meet your needs, you can use the predefined data types
as building blocks to develop your own custom data management class.Integer data type is a wrapper for the System.Int32 structure. One of the members of the Int32 structure is MaxValue, which returns the maximum numeric value allowed for this data type. Thus, even though MaxValue is not officially part of VB, the Integer data type's full dependence on the Int32 data type allows the following usage: Dim usesInt32 As Integer
MsgBox(usesInt32.MaxValue) ' Displays 2147483647Dim targetValue As Integer = 5
5 in memory at the address of targetValue." Because it appears on the left side of an assignment operator, the variable (or its memory location) is sometimes called an l-value.Const keyword:accessModifier Const name As type = value
Option Strict is On, all constant declarations must have a declared type.Byte, Integer, Long, or Short, and also—in 2005 or beyond—SByte, UInteger, ULong, or UShort). The enumeration members are shared and read-only for the lifetime of the application. Public Enum VehicleType As Integer
bicycle = 2
tricycle = 3
passengerCar = 4
eighteenWheeler = 18
End Enum Dim whatIDrive As VehicleType
whatIDrive = VehicleType.passengerCar ' Implicit constructor: No initial size and no initialization
Dim days( ) As Integer
' Explicit constructor: No initial size and no initialization
Dim days( ) As Integer = New Integer( ) {}
' Implicit constructor: Initial size but no initialization
Dim days(6) As Integer
' Explicit constructor: Initial size but no initialization
Dim days( ) As Integer = New Integer(6) {}
' Implicit constructor: Initial size implied by initialization
Dim days( ) As Integer = {1, 2, 3, 4, 5, 6, 7}
' Explicit constructor, Initial size and initialization
Dim days( ) As Integer = New Integer(6) {1, 2, 3, 4, 5, 6, 7}Dim myArray(5) As Integer
Dim rectArray(,) As Integer = {{1, 2, 3}, {4, 5, 6}} Debug.Write(rectArray(0, 0))
Debug.Write(rectArray(0, 1))
Debug.WriteLine(rectArray(0, 2))
Debug.Write(rectArray(1, 0))
Debug.Write(rectArray(1, 1))
Debug.WriteLine(rectArray(1, 2))
' ----- The output is:
123
456ReDim statement.ReDim [Preserve]arrayName(newUpperBound)
Preserve qualifier retains any existing values in the array; all array elements are cleared in the absence of this qualifier. When using Collection class's members are especially useful. Dim states As New Collection
states.Add("New York", "NY")
states.Add("Michigan", "MI")For Each...Next construct. Dim oneState As String
For Each oneState In states
MsgBox(oneState)
Next oneState Public Function RepeatString(ByVal origText As String, _
ByVal howManyTimes As Integer) As String
' ----- Return a string concatenated to itself many times.
Dim counter As Integer
RepeatString = ""
For counter = 1 To howManyTimes
RepeatString &= origText
Next counter
End FunctionRepeatString function includes two parameters, origText and howManyTimes. Each parameter includes a data type and a passing
method. The passing method is either ByVal ("by value") or ByRef ("by reference"). In .NET, the default parameter passing method is ByVal.RepeatString function: a string ("abc") and an integer (5). targetString = RepeatString("abc", 5)ByVal or ByRef keyword is used with a parameter. When data is passed by value, a copy of the source expression or variable is sent to the target procedure. While in that procedure, the parameter acts just like a local variable; it can be examined and modified within the procedure, and it disappears when the procedure is finished. Any changes made to a ByVal parameter in the procedure are not reflected in the source variable. This is clearest when working with value types. Consider the following code. Public Sub ParentRoutine( )
Dim sourceValue As Integer = 5
ChildRoutine(sourceValue)
MsgBox(sourceValue) ' --> Displays "5"
End Sub
Public Sub ChildRoutine(+ (Addition)
result = expression1 + expression2
+ operator acts like the & string concatenation operator, as described below.+ (Unary Plus)
+ operator only appears as a binary operator. But it can be used in a unary form. In this usage, when placed immediately before a number or numeric expression, it ensures that the expression retains its sign, either positive or negative. Since expressions retain their sign by default, the unary plus operator is redundant and rarely used.result =+expression