


#Parameterized constructor code
Since there is nothing in VBA itself that requires the use of these functions, a strong and consistent naming convention greatly improves code quality. There are some naming conventions I want to point out in the above code. NewSchoolObject.Init SchoolDist, DistrictID Here is an example of one of my factory functions: Function NewSchoolObject(SchoolDist As String, _ For example, in my TaxColl2k application, I have a StringFunctions module that I keep in sync with my code library and a tcFactory module whose code has no value outside of the TaxColl2k application. I name this module, " xxFactory," where the "xx" is an application-specific prefix that distinguishes the module from other modules that I import from my standard code library. To work around this shortcoming in VBA, I create a standard module that holds all the parameterized constructors for each of my applications. Factories to the Rescue "In object-oriented programming (OOP), a factory is an object for creating other objects." ( Wikipedia) This differs from almost every other object-oriented programming language, as most OOP languages support parameterized constructors. Thus, it is impossible in VBA to require calling code to provide arguments when creating a class. Unfortunately, there is no way to pass parameters to the Class_Initialize handler. VBA does support default constructors, in the form of the Class_Initialize lifecycle handler: "If a class defines a Class_Initialize lifecycle handler, that subroutine will be invoked as a method each time an instance of that class is created." The VBA language specification does not allow for parameterized constructors. A constructor that does not require arguments is known as a default constructor. It prepares the new object for use, often accepting arguments that the constructor uses to set required member variables.Ī constructor that accepts arguments is known as a parameterized constructor. If you are familiar with other object-oriented programming languages, you are likely familiar with the concept of constructors: In class-based object-oriented programming, a constructor (abbreviation: ctor) is a special type of subroutine called to create an object.
