Classes in Visual Basic 6 (VBA)

Edit ArticleEdit Article

Class is a fundamental concept of Object Oriented Programming (OOP). Class can be considered as repository for storing data in class level variables, and providing functions, properties and exposing events.

Classes are created in the class modules

Adding new class module
Adding new class module

Classes must have a unique name which can be defined in the Visual Basic Editor

Name of a class
Name of a class

MyClass Class Module

Public Var1 As String
Public Var2 As Double

Public Sub Foo()
End Sub

Macro11 Module

Dim cl1 As MyClass
Dim cl2 As MyClass

Set cl1 = New MyClass
Set cl2 = New MyClass

cl1.Var1 = "A"
cl2.Var1 = "B"

cl1.Var2 = 1
cl2.Var2 = 2

Classes are similar to modules, but there are several differences:

  • It is required to create an instance of a class using new keyword
  • All the data associated with this class wil be stored within its instance, which means that different instances of the same class may have different data.
  • Classes allow to handle and expose events

Product of Xarial Product of Xarial