Notes on VBA Class Modules

Instantiating a Class
in the calling module
    Sub DoSomething()
        Dim myClass1 As New Class1
        myClass1.DoSomething
    End Sub
    
Class Module Methods
procedures can be public or private
private procedures can only be called from within the Class Module
    Public Sub Print(arg As String)
        Debug.Print arg
    End Sub

    Public Function Calculate(arg As Double) As Double
        Calculate = arg
    End Function

    Private Function GetValue() As Double
        GetValue = 12.34
    End Function       
     
Class Module Member Variables
class variables can only be private
  
    Private dbl As Double

    Public Sub SetBalance(arg as double)
        dbl = arg
        Debug.Print Balance
    End Sub
        
Class Module Properties
    Private 
    Public Property Get () As Type
    End Property

    Public Property Let (varname As Type )
    End Property

    Public Property Set (varname As Type )
    End Property
          
    Private someArray as Varient

    Public Property Get SomeCount()
        SomeCount = UBound(someArray) + 1
    End Property
         
Let and Get can used as variables in calling code
    Private dSomeValue As Double

    Property Get SomeValue() As Long
         SomeValue = dSomeValue 
    End Property

    Property Let SomeValue(dValue As Long) 
         SomeValue = dSomeValue 
    End Property        
          
value property
 
    Private sName As String

    ' Get/Let Properties
    Property Get Name() As String
        Name = sName
    End Property

    Property Let Name(arg As String)
        sName = qrg
    End Property        
        
object property
    Private collPrices As Collection

    ' Get/Set Properties
    Property Get Prices() As Collection
        Set Prices = collPrices 
    End Property

    Property Set Prices(arg As Collection)
        Set collPrices = arg
    End Property        
         
Class Module Events
    Private Sub Class_Initialize()

    End Sub

    Private Sub Class_Terminate()

    End Sub
    
My First Class
the class
  
    Private iCount As Integer

    Public Property Get Count() As Integer
        Count = iCount
    End Property

    Public Property Let Count(Value As Integer)
        iCount = Value
    End Property

    Private Sub Class_Initialize()
        iCount = 23
    End Sub

    Public Sub Increment()
        iCount = iCount + 1
    End Sub
        
the calling code
 
    Dim myClass1 As Class1

    Private Sub CommandButton1_Click()
        If myClass1 Is Nothing Then
            Set myClass1 = New Class1
        End If
        MsgBox myClass1.Count
        Call myClass1.Increment
        MsgBox myClass1.Count    
    End Sub        
        
Index
n4jvp.com