Initialising a class

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,924
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
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:

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
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.
 
Upvote 0

Forum statistics

Threads
1,223,879
Messages
6,175,148
Members
452,615
Latest member
bogeys2birdies

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