Cover | Table of Contents
Implements keyword.
Dim i As Integer Dim j As Integer i = 5 j = i
Dim i As Integer Dim j As Integer i = 5 j = i
Dim x As Integer
Dim x As Integer = New Integer( )
Dim x As Long, i, j, k As Integer, s As String
Dim x As Integer = 5
Dim x As Integer = 6, y As Integer = 9
Dim obj As MyClass
Nothing at this point. Object
creation requires an explicit call to the object's constructor,
as in:
Dim obj As New MyClass( )
Dim obj As MyClass = New Myclass( )
Dim obj As MyClass obj = New MyClass( )
Dim ui As UInt16 ui = Convert.ToUInt16(65535) MsgBox(ui.ToString)
Dim i As Integer MsgBox(i.Maxvalue) ' Displays 2147483647
arr:S1 x S2 x ... x SN
→ T
Si={0,1,...,Ki}
arr:{0,1,...,5} → T
arr:{0,1,...,5}x{0,1,...,8} → T
Dim APerson As CPerson
Dim rs As ADO.Recordset
Dim obj As MyClass
New keyword, as
in:
Dim obj As MyClass = New Myclass( )
Dim obj As MyClass obj = New MyClass( )
Dim obj As New MyClass( )
Set
statement, which is no longer supported.)
Dim obj As Class1
Dim colStates As New Collection
colStates.Add("New York", "NY")
colStates.Add("Michigan", "MI")
For
Each...Next
construct. Also like arrays, collection members are accessible by
their index value, although the lower bound of a collection
object's index is always 1.
Function RepeatString(ByVal sInput As String, ByVal iCount As Integer) _
As String
Dim i As Integer
For i = 1 To iCount
RepeatString = RepeatString & sInput
Next
End Function
s = RepeatString("Donna", 4)
ByVal
in front of each parameter. This
specifies that arguments are passed by value to this function.
Passing by value means that the actual value of
the argument is passed to the function. This is relevant when an
argument is a variable. For instance, consider the following code:
Sub Inc(ByVal x As Integer)
x = x + 1
End Sub
Dim iAge As Integer = 20
Inc(iAge)
Msgbox(iAge)
Msgbox(iAge)
Inc(iAge)
Inc by value. Since only the value (in this case
20) is passed, that value is assigned to a local variable named
x within the procedure. This local
variable is increased to 21, but once the procedure ends, the local
variable is destroyed. The variable Public Class ClassName End Class
Class...End
Class construct that marks the beginning and end
of a class definition. Thus, the code for more than one class as well
as one or more code modules (which are similarly delimited by the
Module...End Module construct)
can be contained in a single source code file.
' Employee class
Public Class CEmployee
' Salary property is read/write
Private mdecSalary As Decimal
Property Salary( ) As Decimal
Get
Salary = mdecSalary
End Get
Set
mdecSalary = Value
End Set
End Property
Public Overridable Sub IncSalary(ByVal sngPercent As Single)
mdecSalary = mdecSalary * (1 + CDec(sngPercent))
End Sub
End Class
' Executive Class
Public Class CExecutive
Inherits CEmployee
' Calculate salary increase based on 5% car allowance as well
Overrides Sub IncSalary(ByVal sngPercent As Single)
Me.Salary = Me.Salary * CDec(1.05 + sngPercent)
End Sub
End Class
Inherits CEmployee
' Employee class
Public Class CEmployee
. . .
Public Overridable Sub IncSalary(ByVal sngPercent As Single)
End Sub
End Class
MustOverride
keyword, as shown here:
' Employee class
Public MustInherit Class CEmployee
. . .
Public MustOverride Sub IncSalary(ByVal sngPercent As Single)
End Class
MustOverride,
there is no End
Sub statement
associated with the method. Note also that when using the
MustOverride keyword, Microsoft requires that the
class be declared with the
MustInherit
keyword. This specifies that we cannot
create objects of type CEmployee.
Overloads Public Shared Function Abs(Decimal) As Decimal Overloads Public Shared Function Abs(Double) As Double Overloads Public Shared Function Abs(Integer) As Short Overloads Public Shared Function Abs(Integer) As Integer Overloads Public Shared Function Abs(Long) As Long Overloads Public Shared Function Abs(SByte) As SByte Overloads Public Shared Function Abs(Single) As Single
Overloads
keyword, which tells VB that this
function is overloaded.
Public
Private
Friend
Protected
Protected
Friend
Public,
Private, and Friend are
allowed.)
Public,
Private, or Friend
(Protected is not allowed). When a class module
declaration specifies one of these access modifiers, this simply
restricts all of its members to that level of access, unless a
member's access is further restricted by the access modifier on
the member declaration itself. For instance, if the class has
Friend access, no member can have
Public access. (Put another way, the
Public access is overridden by the
Friend class access.)
Imports System ' Optional since System is always imported
Dim array1( ) As Integer = {1, 2, 3, 4}
Dim array2(3) As Integer
Array.Copy(array1, array2, 3)