Instantiating variables

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,832
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
If I have a class with a property, I must instantiate it before using it.

So either:

Code:
Dim UseClass As MyClass
Set UseClass = New MyClass

Msgbox UseClass.Age = 10

or

Code:
Dim UseClass As New MyClass

Msgbox UseClass.Age = 10

I don't understand why the following code works.

This is in a class called Holiday:

Code:
Option Explicit

Public CountryName As String
Public ResortName As String
Public DaysLength As Integer
Public TravelMethod As String
Public Price As Currency
Public HolidayId As String

This is in another class called Holdiays:

Code:
Option Explicit

Private p_Holidays As Collection
Private Sub Class_Initialize()
 
Set p_Holidays = New Collection

End Sub

Sub Add(ThisHoliday As Holiday, HolidayId As String)

    p_Holidays.Add Item:=ThisHoliday, key:=HolidayId

End Sub

Public Property Get Item(HolidayId As Variant) As Holiday

    Set Item = p_Holidays(HolidayId)

End Property

Public Sub Remove(HolidayId As Variant)

    p_Holidays.Remove (HolidayId)

End Sub

Public Property Get Count() As Long

    Count = p_Holidays.Count

End Property

This is in a standard module:

Code:
Option Explicit

Dim SummerHolidays As Holidays

Sub DemonstrateClass()

    ReadHolidays

    WriteHolidays

End Sub

Sub ReadHolidays()
   
    Dim EachCell As Range
    Dim h As Holiday

    Set SummerHolidays = New Holidays

    Range("A4").Select
    Range(ActiveCell, ActiveCell.End(xlDown)).Select

    For Each EachCell In Selection

        Set h = New Holiday

        h.CountryName = EachCell.Value
        h.ResortName = EachCell.Offset(0, 1).Value
        h.DaysLength = EachCell.Offset(0, 2).Value
        h.TravelMethod = EachCell.Offset(0, 3).Value
        h.Price = EachCell.Offset(0, 4).Value
        h.HolidayId = EachCell.Offset(0, 5).Value

        SummerHolidays.Add h, h.HolidayId

        Set h = Nothing

    Next EachCell

End Sub

Sub WriteHolidays()

    Dim h As Holiday
    Dim HolidayNumber As Integer

    For HolidayNumber = 1 To SummerHolidays.Count

        '*************************************************
        Set h = SummerHolidays.Item(HolidayNumber)
        '*************************************************

        Debug.Print "Holiday " & h.HolidayId & " to " & h.ResortName & " in " & h.CountryName & " by " & h.TravelMethod & _
        " lasts for " & h.DaysLength & " days and costs " & Format(h.Price, "?#,##0.00")

    Next HolidayNumber

End Sub

In Sub WriteHolidays, we have:

Code:
Dim h As Holiday

but it seems it has not been instantiated because I don't see:

Code:
Set h = New Holiday

Instead, this line follows:

Code:
Set h = SummerHolidays.Item(HolidayNumber)

Can someone please explain why the variable h looks like it has not been instantiated, yet can still be used?

Thanks
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
h is declared to be a Holiday, but we don't know which Holiday yet.

VBA Code:
Set h = New Holiday
says that h is a brand new instance of a Holiday

VBA Code:
Set h = SummerHolidays.Item(HolidayNumber)
says that h is the an already existing instance of Holiday.

One doesn't instantiate variables, one instantiates objects. And then sets variables to that object.
The SummerHolidays.Item(HolidayNumber) was instantiated in the ReadHolidays sub (and stored in a collection)

This is where one has to be mindful of the difference between an object and a variable that refers to that object.
 
Upvote 0
h is declared to be a Holiday, but we don't know which Holiday yet.

VBA Code:
Set h = New Holiday
says that h is a brand new instance of a Holiday

VBA Code:
Set h = SummerHolidays.Item(HolidayNumber)
says that h is the an already existing instance of Holiday.

One doesn't instantiate variables, one instantiates objects. And then sets variables to that object.
The SummerHolidays.Item(HolidayNumber) was instantiated in the ReadHolidays sub (and stored in a collection)

This is where one has to be mindful of the difference between an object and a variable that refers to that object.

Thanks for your explanation.
 
Upvote 0

Forum statistics

Threads
1,214,627
Messages
6,120,610
Members
448,973
Latest member
ChristineC

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