Class Definition

A class is a data structure that contains the following kinds of members:

constructors

constants

variables

fields (public variables)

properties

methods

 

Implementation of the Members
Namespace = ClassDemo
Class AClass
   Private MyArray as String()                    [Variable] 
   Private MyVariable As Integer = 34
   Public Sub New(I as Integer)                   [Constructor]
      ReDim MyArray(I)
   End Sub 
   
     Private Const MyConst As Integer = 12         [Constant]
   
   
   Public Function MyMethod(I as Integer) as Integer      [Method]
      Return MyConst * I
   End Sub 
   
   Public ThisField As Integer                    [Field]
 
     Public Property MyProperty() As Integer               [Property]     
      Get
         Return MyVariable 
                    End Get 
                    Set (ByVal Value As Integer)
         MyVariable = value
      End Set
   End Property
   
   Private m_PropertyValue As Integer = 0         [Property with                                                                                                    internal procedure]
   Public Property ThisProperty() As Integer   
      Get
         Return m_PropertyValue  
      End Get
      Set(ByVal Value As Integer)
         ' Only allow Set operation for values less than 10.
         If Value < 10 Then m_PropertyValue = Value
      End Set
   End Property
 
   Private PropertyValues As String()
   ' Define the default property.
   Default Public Property Prop1(ByVal Index As Integer) As String
      Get
         Return PropertyValues(Index)
      End Get
      Set(ByVal Value As String)
         If PropertyValues Is Nothing Then
            ' The array contains Nothing when first accessed.
            ReDim PropertyValues(0)
         Else
            ' Re-dimension the array to hold the new element.
            ReDim Preserve PropertyValues(UBound(PropertyValues) + 1)
         End If
         PropertyValues(Index) = Value
      End Set
   End Property
End Class
 
 
Use of the Members
Imports ClassDemo                               
Sub MyUseofMembers()
Private ThisCalculation as Integer
Dim objA As new AClass(10)                     [Constructor]
ThisCalculation = objA.MyMethod(16)            [Method]
objA.ThisField = ThisCalculation                                             [Field]
        ThisCalculation = 6*objA.MyProperty           [Property]
end Sub
 
Sub TestPropertyLT10()                        [Property with                                                         internal procedure
   ' Assume, for this example, that the only valid values for
   ' ThisProperty are numbers less than 10.
   objA.ThisProperty = 36
   ' The next statement will print the original value of 0 instead.
   MsgBox("ThisProperty = " & objA.ThisProperty)
     objA.ThisProperty = 6
     objA.ThisProperty = 25
     ' The next statement will print the replaced value of 6.
   MsgBox("ThisProperty = " & objA.ThisProperty)
End Sub
 
Sub TestDefaultProperty()
Dim C As New Class2()
' The first two lines of code access a property the standard way.
C.Prop1(0) = "Value One"    ' Property assignment.
MessageBox.Show(C.Prop1(0)) ' Property retrieval.
 
' The following two lines of code use default property syntax.
C(1) = "Value Two"     ' Property assignment.
MessageBox.Show(C(1))  ' Property retrieval.
 
     Default Public Property Item(index As Integer) As Integer
      Get
         Return 0 End Get Set (ByVal Value As Integer)
         Console.WriteLine(“item(“ & index & “) = “ & value)
      End Set
   End Property
Public Event MyEvent()
   Friend Class MyNestedClass
   End Class 
 
End Class 

 

The following example shows uses of these members:

Module Test
   Dim WithEvents aInstance As AClass   
   Sub Main()
      ‘ This shows constructor usage.
      Dim a As New AClass()
      Dim b As New AClass()
 
      ‘ This shows constant usage.
      Console.WriteLine(“MyConst = “ & AClass.MyConst)
      ‘ This shows variable usage.
      a.MyVariable += 1
      Console.WriteLine(“a.MyVariable = “ & a.MyVariable)
 
      ‘ This shows method usage.
      a.MyMethod()
 
      ‘ This shows property usage.
      a.MyProperty += 1
      Console.WriteLine(“a.MyProperty = “ & a.MyProperty)
      a(1) = 2
 
      ‘ This shows event usage.
      aInstance = a
   End Sub 
   Sub MyHandler Handles aInstance.MyEvent
      Console.WriteLine(“Test.MyHandler”)
   End Sub 
End Module