tiredofit
Well-known Member
- Joined
- Apr 11, 2013
- Messages
- 1,834
- Office Version
- 365
- 2019
- Platform
- Windows
If I have a class with a property, I must instantiate it before using it.
So either:
or
I don't understand why the following code works.
This is in a class called Holiday:
This is in another class called Holdiays:
This is in a standard module:
In Sub WriteHolidays, we have:
but it seems it has not been instantiated because I don't see:
Instead, this line follows:
Can someone please explain why the variable h looks like it has not been instantiated, yet can still be used?
Thanks
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