> ## Content Index
> Fetch the complete content index at: https://nolongerset.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Parameterized Constructors in VBA
- URL: https://nolongerset.com/parameterized-constructors-in-vba/
- Published: 2021-03-21T17:24:15.000Z
- Updated: 2026-05-08T13:05:46.000Z
- Description: VBA does not allow for parameterized constructors. We can work around that, though, using the Factory pattern and a strong naming convention.
- Author: Mike Wolfe
- Tags: Intermediate, Conventions, #Import 2026-05-20 02:59

If you are familiar with other object-oriented programming languages, you are likely familiar with the concept of [constructors](https://en.wikipedia.org/wiki/Constructor%5F%28object-oriented%5Fprogramming%29):

> In [class-based](https://en.wikipedia.org/wiki/Class-based%5Fprogramming) [object-oriented programming](https://en.wikipedia.org/wiki/Object-oriented%5Fprogramming), a **constructor** (abbreviation: **ctor**) is a special type of [subroutine](https://en.wikipedia.org/wiki/Subroutine) called to [create an object](https://en.wikipedia.org/wiki/Object%5Fcreation). It prepares the new object for use, often accepting [arguments](https://en.wikipedia.org/wiki/Parameter%5F%28computer%5Fprogramming%29) that the constructor uses to set required [member variables](https://en.wikipedia.org/wiki/Member%5Fvariable).

A constructor that accepts arguments is known as a [parameterized constructor](https://en.wikipedia.org/wiki/Constructor%5F%28object-oriented%5Fprogramming%29#Parameterized%5Fconstructors). A constructor that does not require arguments is known as a [default constructor](https://en.wikipedia.org/wiki/Constructor%5F%28object-oriented%5Fprogramming%29#Default%5Fconstructors).

The VBA language specification does not allow for parameterized constructors.

VBA does support default constructors, in the form of the [**Class\_Initialize** lifecycle handler](https://docs.microsoft.com/en-us/openspecs/microsoft%5Fgeneral%5Fpurpose%5Fprogramming%5Flanguages/ms-vbal/62bbe63e-379c-4dc0-8648-7d9050a2f396):

> "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..."

Unfortunately, there is no way to pass parameters to the **Class\_Initialize** handler. Thus, it is impossible in VBA to require calling code to provide arguments when creating a class. This differs from almost every other object-oriented programming language, as most OOP languages support parameterized constructors.

## Factories to the Rescue

> "In [object-oriented programming (OOP)](https://en.wikipedia.org/wiki/Object-oriented%5Fprogramming), a **factory** is an [object](https://en.wikipedia.org/wiki/Object%5F%28computer%5Fscience%29) for [creating other objects](https://en.wikipedia.org/wiki/Object%5Fcreation)..." *([Wikipedia](https://en.wikipedia.org/wiki/Factory%5F%28object-oriented%5Fprogramming%29))*

To work around this shortcoming in VBA, I create a standard module that holds all the parameterized constructors for each of my applications. 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. For example, in my TaxColl2k application, I have a **StringFunctions** module that I keep in sync with my [code library](https://nolongerset.com/code-library/) and a **tcFactory** module whose code has no value outside of the TaxColl2k application.

### Example factory function

Here is an example of one of my factory functions:

```vba
Function NewSchoolObject(SchoolDist As String, _
                         DistrictID As String) As oSchool
    Set NewSchoolObject = New oSchool
    NewSchoolObject.Init SchoolDist, DistrictID
End Function
```

### Naming conventions

There are some naming conventions I want to point out in the above 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](https://www.joelonsoftware.com/2005/05/11/making-wrong-code-look-wrong/).

The underlying object is named **oSchool**. I use a variety of prefixes when naming my class modules. The "o" prefix is reserved for classes that represent real-world objects. These kinds of objects in my applications frequently have a one-to-one relationship with database tables, too.

The name of the factory function comprises three parts:

- "New": a literal string that prefixes every one of my factory function names
- "School": the underlying object name stripped of its prefix
- "Object": a literal string that I append to each factory function name

Thus, every factory function name follows this format: **New** *{class name}* **Object**.

Finally, the underlying class has a public method named **Init** that takes the same parameters as the factory function. The **Init** method must be public, but it should only ever be called from a factory function.

### Usage

Here's how the code looks without using the factory function:

```vba
Dim School As oSchool
Set School = New oSchool
School.SchoolDist = "X"
School.DistrictID = 42
```

Here's the same code with the factory function:

```vba
Dim School As oSchool
Set School = NewSchoolObject("X", 42)
```

*Image by [LEEROY Agency](https://pixabay.com/users/life-of-pix-364018/?utm%5Fsource=link-attribution&utm%5Fmedium=referral&utm%5Fcampaign=image&utm%5Fcontent=406905) from [Pixabay](https://pixabay.com/?utm%5Fsource=link-attribution&utm%5Fmedium=referral&utm%5Fcampaign=image&utm%5Fcontent=406905)*