Home > Articles

  • Print
  • + Share This
This chapter is from the book

Class Initialization

As we showed previously, you can use the Class_Initialize event to set up default values for some class variables. However, if you want to set up some values that are specific for each instance (such as our swimmer's names and times), we need a standard way to do this. In other languages, classes have special methods called constructors that you can use to pass in useful data at the same time you create the instance. Since VB6 classes lack these methods, we introduce the convention of an init method that we'll use to pass in instance specific data.

In our preceding Swimmer class, note that we have an init method that in turn calls the init method of the Tokenizer class.

Public Sub init(dataline As String)
Dim tok As New Tokenizer

tok.init dataline           'initialize string tokenizer

Other languages, including VB7, also allow classes to have a series of constructors that each have different arguments. Since this is not a feature of VB6, we'll use various setXXX methods instead.

  • + Share This
  • 🔖 Save To Your Account