Class Factory

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,832
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Class Factories have been suggested as a workaround for VBA's lack of a Constructor.

This is what I've come up with:

Class1:

Code:
Option Explicit
    
    Private pColour As String
    Private pMake As String
    
Public Property Get Colour() As String

    Colour = pColour
    
End Property

Public Property Let Colour(ByVal C As String)

    pColour = C
    
End Property

Public Property Get Make() As String

    Make = pMake
    
End Property

Public Property Let Make(ByVal M As String)

    pMake = M
    
End Property

Private Sub Class_Initialize()

    Me.Colour = Col
    Me.Make = Mak
        
End Sub

Standard module:

Code:
Option Explicit

    Public Col As String
    Public Mak As String
    
Sub Test()
    
    Col = "Blue"
    Mak = "Mini"
    
    Dim abc As Class1
    Set abc = New Class1
    
    Debug.Print Col
    Debug.Print Mak
    
    Col = "Red"
    Make = "Ford"
    
    Dim cde As Class1
    Set abc = New Class1
    
    Debug.Print Col
    Debug.Print Mak
    
End Sub

Is this the same as a Class Factory?

Thanks
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Not quite... :)

A Class Factory is simply a method whereby a function creates an object based on a given type.

So, for example, let's say that we have two classes, clsCar and clsTruck. We would call this function, and pass it a string to specify which new object to create.

If we pass it the string "car", we would want it to create and return a new car object. And, if we pass it the string "truck", we would want it to create and return a new truck object.

And, if by mistake we pass it some other unrecognized string, we would want it to return Nothing so that we could check whether it was unsuccessful.

So the function might look something like this...

VBA Code:
Function ClassFactory(ByVal class_type As String) As Object

    Dim new_obj As Object

    Select Case LCase(class_type)
        Case "car"
            Set new_obj = New clsCar
        Case "truck"
            Set new_obj = New clsTruck
    End Select
    
    Set ClassFactory = new_obj
    
End Function

Now we could create a new car object as follows...

VBA Code:
    Dim new_car As clsCar
    Set new_car = ClassFactory("car")

And, we could check whether a new car object was in fact created...

VBA Code:
    If Not new_car Is Nothing Then
        MsgBox "Created a new car object.", vbInformation
    Else
        MsgBox "Unable to create a new car object", vbInformation
    End If

Similarly, we could create a new truck object as follows...

VBA Code:
    Dim new_truck As clsTruck
    Set new_truck = ClassFactory("truck")

And, again, we could check whether a new truck object was created...

VBA Code:
    If Not new_truck Is Nothing Then
        MsgBox "Created a new truck object.", vbInformation
    Else
        MsgBox "Unable to create a new truck object.", vbInformation
    End If

Now, we could take it a step further. In addition to creating a new object, we can have our function initialize it as well. Have a look at "Class Module Example 2" on the following page...


Hope this helps!
 
Upvote 0
Not quite... :)

A Class Factory is simply a method whereby a function creates an object based on a given type.

So, for example, let's say that we have two classes, clsCar and clsTruck. We would call this function, and pass it a string to specify which new object to create.

If we pass it the string "car", we would want it to create and return a new car object. And, if we pass it the string "truck", we would want it to create and return a new truck object.

And, if by mistake we pass it some other unrecognized string, we would want it to return Nothing so that we could check whether it was unsuccessful.

So the function might look something like this...

VBA Code:
Function ClassFactory(ByVal class_type As String) As Object

    Dim new_obj As Object

    Select Case LCase(class_type)
        Case "car"
            Set new_obj = New clsCar
        Case "truck"
            Set new_obj = New clsTruck
    End Select
 
    Set ClassFactory = new_obj
 
End Function

Now we could create a new car object as follows...

VBA Code:
    Dim new_car As clsCar
    Set new_car = ClassFactory("car")

And, we could check whether a new car object was in fact created...

VBA Code:
    If Not new_car Is Nothing Then
        MsgBox "Created a new car object.", vbInformation
    Else
        MsgBox "Unable to create a new car object", vbInformation
    End If

Similarly, we could create a new truck object as follows...

VBA Code:
    Dim new_truck As clsTruck
    Set new_truck = ClassFactory("truck")

And, again, we could check whether a new truck object was created...

VBA Code:
    If Not new_truck Is Nothing Then
        MsgBox "Created a new truck object.", vbInformation
    Else
        MsgBox "Unable to create a new truck object.", vbInformation
    End If

Now, we could take it a step further. In addition to creating a new object, we can have our function initialize it as well. Have a look at "Class Module Example 2" on the following page...


Hope this helps!

Thanks for your explanation.

The reason I thought my code resembled a class factory was according to this article:

Code:
https://datapluscode.com/general/no-constructor-no-problem/

it states:

Code:
When an object is created using this class, the class internally sets the colour of the car to be red when created. If we wanted to create a Car object which was blue, we would have to set its colour property to blue after creation:

But what if we wanted to do this before it is first used?

so my code indeed does (I think!) enable the user to specify the colour and make before it is used.

If you really want to get into this sort of level, I'd strongly suggest reading the rubberduck vba blog pages - eg: Factories: Parameterized Object Initialization

Do I really want to go to this level?

I'm erring on the side of caution, on the off chance I have to read someone else's code that does something like this and therefore need to know what's going on.

I'm concerned that if I choose to write VBA in an OO way, then I would need to know about inheritance, composition, etc. which is beyond me.

Would I be correct to conclude that the majority of people who do code VBA in an OO way have had some prior exposure to an OO language, such as C#?
 
Upvote 0
Do I really want to go to this level?

I have no idea. I don't know what you do.

Would I be correct to conclude that the majority of people who do code VBA in an OO way have had some prior exposure to an OO language, such as C#?

The majority of people don't really code in VBA in anything resembling an OOP way, (I very rarely do as it would massively overcomplicate what I need) but I would imagine that many of those who do have had exposure to some other OO language.
 
Upvote 0
I have no idea. I don't know what you do.



The majority of people don't really code in VBA in anything resembling an OOP way, (I very rarely do as it would massively overcomplicate what I need) but I would imagine that many of those who do have had exposure to some other OO language.


Thanks for your reply, made me feel a lot better!

I work in finance and I agree the majority of the VBA code is procedual. Even if I understand the principles and examples in books on VBA oop, when given a problem to solve, my brain doesn't immediate jump towards oop. Perhaps it's because I've been exposed to procedual for far too long.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,622
Messages
6,120,580
Members
448,972
Latest member
Shantanu2024

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top