tiredofit
Well-known Member
- Joined
- Apr 11, 2013
- Messages
- 1,924
- Office Version
- 365
- 2019
- Platform
- Windows
Further to this post:
the code is as follows:
APPROACH 1.
and:
When the above code is run, the moment this line is reached:
it jumps to the Class_Initialize, WITHOUT passing any arguments.
But if I wrote the code, "in the usual way", like this:
APPROACH 2.
and:
when this line is reached:
it also jumps to the Class_Initialize, WITHOUT passing any arguments.
So how is the first approach "passing arguments" when initialising?
Thanks
Pass argument to class initialize
This code is taken from here: https://newbedev.com/pass-arguments-to-constructor-in-vba I can't get it to work. My understanding is as follows: This goes into a standard module: Sub Test Dim a As Employee Set a = Factory.CreateEmployee("Excel", 100) End Sub This goes...
www.mrexcel.com
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: