Initialising a class

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,080
This code:

Rich (BB code):
<code style="color: rgb(0, 96, 0);">Option Explicit</code> <code style="color: rgb(0, 96, 0);">Private pKey As Long</code> <code style="color: rgb(0, 96, 0);">Private pName As String</code> <code style="color: rgb(0, 96, 0);">Private pChildren As Collection</code> <code style="color: rgb(0, 96, 0);">Public Property Get Key() As Long</code> <code style="color: rgb(0, 96, 0);"> Key = pKey</code> <code style="color: rgb(0, 96, 0);">End Property</code> <code style="color: rgb(0, 96, 0);">Public Property Get Name() As String</code> <code style="color: rgb(0, 96, 0);"> Name = pName</code> <code style="color: rgb(0, 96, 0);">End Property</code> <code style="color: rgb(0, 96, 0);">Public Property Get Children() As Collection</code> <code style="color: rgb(0, 96, 0);"> Set Children = pChildren</code> <code style="color: rgb(0, 96, 0);">End Property</code> <code style="color: rgb(0, 96, 0);">Public Property Let Key(p As Long)</code> <code style="color: rgb(0, 96, 0);"> pKey = p</code> <code style="color: rgb(0, 96, 0);">End Property</code> <code style="color: rgb(0, 96, 0);">Public Function Init(k As Long, sName As String) As cMyClass</code> <code style="color: rgb(0, 96, 0);"> pKey = k</code> <code style="color: rgb(0, 96, 0);"> pName = sName</code> <code style="color: rgb(0, 96, 0);"> Set pChildren = New Collection</code> <code style="color: rgb(0, 96, 0);"> Set Init = Me</code> <code style="color: rgb(0, 96, 0);">End Function </code>
<code style="color: rgb(0, 96, 0);">
</code>
<code style="color: rgb(0, 96, 0);">

is taken from here:

Rich (BB code):
http://ramblings.mcpher.com/Home/excelquirks/snippets/classes



The author states:

Rich (BB code):
"
A method is a procedure that operates on a class, sometimes returning some value. Usually when you create a class you want to populate it with some initial values, and perhaps set up some kind of other structures. I generally create a method called "Init"to do that. Other languages have special methods called constructors that are called on the instantiation of a class. I kind of like the idea of initializing it when I want to. "


My question is: why not just use:

Rich (BB code):
Private Sub Class_Initialize()
End Sub



Is it because Initialize always initialises at the start but he wants to initialise whenever he chooses?

Regardless,

Rich (BB code):
"
Other languages have special methods called constructors that are called on the instantiation of a class."


Surely isn't VBA's Sub Class_Initialize() EXACTLY this?

Thanks




<strike>
</strike>

</code>​
<strike></strike><strike></strike>
 
Last edited:

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
18,919
Like constructors for other languages, such as C++, the initialize event is always executed when an object is instantiated. However, unlike constructors, you cannot pass parameters to the initialize event in VBA. Therefore, to get around this limitation, you can create a method to initialize your object with the desired value or values, as per your example.
 

Forum statistics

Threads
1,077,737
Messages
5,335,914
Members
399,057
Latest member
mgabr

Some videos you may like

This Week's Hot Topics

Top