Pass argument in class initialise

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,924
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Further to this post:


the code is as follows:


APPROACH 1.

Code:
' STANDARD MODULE

Sub Test()

    Dim a As Employee
    Set a = CreateEmployee("Excel", 100)
  
End Sub

Public Function CreateEmployee(name As String, age As Integer) As Employee

    Set CreateEmployee = New Employee
    CreateEmployee.InitiateProperties name:=name, age:=age

End Function

and:

Code:
' CLASS MODULE EMPLOYEE

    Private m_name As String
    Private m_age As Integer
  
Public Sub InitiateProperties(name As String, age As Integer)

    m_name = name
    m_age = age

End Sub

Private Sub Class_Initialize()

End Sub

When the above code is run, the moment this line is reached:

Code:
Set CreateEmployee = New Employee

it jumps to the Class_Initialize, WITHOUT passing any arguments.


But if I wrote the code, "in the usual way", like this:

APPROACH 2.

Code:
'STANDARD MODULE

Sub Test()

    Dim a As Employee
    Set a = New Employee
   
    a.name = "Excel"
    a.age = 10
   
End Sub

and:

Code:
' CLASS MODULE EMPLOYEE

    Private m_name As String
    Private m_age As Integer

Public Property Get name() As String
   
    name = m_name
   
End Property

Public Property Let name(ByVal n As String)

    m_name = n
   
End Property

Public Property Get age() As Integer

    age = m_age
   
End Property

Public Property Let age(ByVal a As Integer)

    m_age = a
   
End Property

Private Sub Class_Initialize()

End Sub

when this line is reached:

Code:
Set a = New Employee

it also jumps to the Class_Initialize, WITHOUT passing any arguments.

So how is the first approach "passing arguments" when initialising?

Thanks
 
Last edited by a moderator:

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
This is a better link: Pass arguments to Constructor in VBA

So how is the first approach "passing arguments" when initialising?

The code isn't passing anything to Class_Initialize. I'm not sure where you got that idea.

It's passing all the arguments required to populate an instance of the Employee class in one go, rather than one by one.

VBA Code:
'Employee Class Module
Private m_name As String
Private m_age As Long
'Compare this:
Public Sub InitiateProperties(Name As String, Age As Long)

    m_name = Name
    m_age = Age

End Sub
'... with the "usual":
Public Property Let Name(s As String)
    m_name = s
End Property
Public Property Let Age(i As Long)
    m_age = i
End Property
 
Upvote 0
Thanks for the link but it's the same as my original post!

"It's passing all the arguments required to populate an instance of the Employee class in one go, rather than one by one."

So is the advantage that it's faster to pass all in one go, as opposed to the standard way?

My interest in this is to compensate for the lack of a constructor in VBA.
 
Last edited by a moderator:
Upvote 0
Thanks for the link but it's the same as my original post!
Yes, same link ultimately. Mine is a direct link to the article where you got your code. Your link above was to a MrExcel thread that provide that link, but didn't add anything useful.

My interest in this is to compensate for the lack of a constructor in VBA.
Yes, that's the key point. VBA doesn't have a constructor, i.e. you can't pass arguments to Class_Initialize. We're all dealing with that.

I'm not clear whether you're trying to code something, or just trying to understand it conceptually. How far you take this is up to you - do you want to run some office/home working code, or build a big factory?

I'd point you to some other articles, but it looks that's been done before: Class Factory
 
Last edited:
Upvote 0
I'm trying to get to grips with the various concepts surrounding object oriented programming in VBA.

I've scoured the internet for what little material there is, most VBA books just mention class modules at best.

Websites that explain OO, as well as books such as the Head First series, tend to focus on Java or C# (understandly because VBA is not totally OO).

I have yet to read anything (geared towards VBA) about how one might plan to write an application in VBA in an OO way, ie how many classes will be needed, what they'll be doing and how might they interact / refer top each other.

Perhaps you could help and give your thoughts on this other post of mine:


Thanks
 
Last edited by a moderator:
Upvote 0
Perhaps you could help and give your thoughts on this other post of mine:
I did see that thread and thought about replying, but didn't because I couldn't make head or tail of the question. If others are also struggling, that may be why you've had no answers.

I think you were talking about different objects, rather than different instances of the same object. And referring to a method, let's say .AddItem, that both objects might have in similar form.

But you totally lost me when you said: Does that mean method1 needs to be copied and relabelled as method2, then "packaged" into object2?
 
Upvote 0
I did see that thread and thought about replying, but didn't because I couldn't make head or tail of the question. If others are also struggling, that may be why you've had no answers.

I think you were talking about different objects, rather than different instances of the same object. And referring to a method, let's say .AddItem, that both objects might have in similar form.

But you totally lost me when you talked said: Does that mean method1 needs to be copied and relabelled as method2, then "packaged" into object2?
I am talking about different objects.

From what I've read, every class should only do one thing alone and be self-contained, so It should have its own methods and properties.

Imagine I have object1 and it has method1 (say which deletes column A) and property1 (say a name, Apple).

But object2 also deletes columns but is called Orange.

As both methods are identical, in the context of self-sufficiency, it seems to me I would have to replicate method1 to put it into object2, which is efficient.

On the other hand, if object2 could somehow use method1, that breaks "the rule" of self-sufficiency.

Hope this makes sense.
 
Upvote 0
It sounds like you are confusing classes and objects.

Your example, which I suspect is oversimplified, would suggest having one class with a method and a Name property. You then create two instances of that class (objects) and assign different values to the Name of each.
 
Upvote 0
It sounds like you are confusing classes and objects.

Your example, which I suspect is oversimplified, would suggest having one class with a method and a Name property. You then create two instances of that class (objects) and assign different values to the Name of each.
Yes, it is oversimplified.

I'll try and dig up a more concrete example.
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,223
Members
452,620
Latest member
dsubash

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