tiredofit
Well-known Member
- Joined
- Apr 11, 2013
- Messages
- 1,924
- Office Version
- 365
- 2019
- Platform
- Windows
The following code is taken from here:
This is in Sheet1
This is in a class called Animal:
This is in a class called Cat:
What I don't understand is when the code reaches this point:
after the line with ************************** is executed, the code jumps to the appropriate class.
How does it know which class to go to?
Code:
https://www.youtube.com/watch?v=rZ96jR_y4gY
This is in Sheet1
Code:
Option Explicit
Sub CreateReport()
Dim animals As New Collection
Dim animal As Variant
Dim choice As String
Dim rng As Range
Dim x As Integer
Dim firstRow
Dim lastRow As Integer
' Set parameters, no such thing as 'Fishs' so don't change that word
If (Range("J5") = "Fish") Then
choice = Range("J5")
Else
choice = Left(Range("J5"), Len(Range("J5")) - 1)
End If
firstRow = 4
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
' Use a loop and parse all of the animals into their respective classes
For x = firstRow To lastRow
Set rng = Range("A" & x & ":H" & x)
Set animal = AnimalClassFactory(rng)
animals.Add animal
Next x
PrintCollection animals, "animals"
printReport animals, choice
End Sub
Sub PrintCollection(coll As Collection, name As String)
Debug.Print vbNewLine & "Printing " & name & ":"
On Error Resume Next
Dim item As Variant
For Each item In coll
Debug.Print item.PrintOut()
Next item
On Error GoTo 0
End Sub
This is in a class called Animal:
Code:
' This is the Interface for the Cat and Dog classes.
' The Cat and Dog classes are required to implement
' these properties as they 'implement Animal'
Option Explicit
Public Property Get name() As String
End Property
Public Property Get Age() As Long
End Property
Public Property Get Weight() As Double
End Property
Public Property Get Talk() As String
End Property
This is in a class called Cat:
Code:
' Cat class derived from Animal interface
Option Explicit
' Implements Animal interface requiring the name, age and weight.
Implements animal
Private name_ As String
Private age_ As Long
Private breed_ As String
Private weight_ As Double
Private legs_ As Integer
Private likes_ As Variant
Private arrivalDate_ As Date
Private readyToHouse_ As Boolean
Private icon_ As Shape
Private meow_ As String ' unique to the Cat class
Private Sub Class_Initialize()
legs_ = 4
meow_ = "Meoww!"
' Set icon_ = Sheets("Icons").Shapes("catPic")
End Sub
Public Sub Init(rng As Range)
name_ = rng.Cells(1, 1)
age_ = rng.Cells(1, 3)
breed_ = rng.Cells(1, 4)
weight_ = rng.Cells(1, 5)
likes_ = Split(CStr(rng.Cells(1, 7).Value), ",")
arrivalDate_ = rng.Cells(1, 6)
If (rng.Cells(1, 8) = "Y") Then readyToHouse_ = True
End Sub
Public Sub PrintOut()
Debug.Print name_, TypeName(Me), age_, breed_, weight_, arrivalDate_, readyToHouse_, legs_, daysHoused;
End Sub
Public Property Get readyToHouse() As Boolean
readyToHouse = readyToHouse_
End Property
Private Sub Class_Terminate()
' Debug.Print "Cat class instance deleted, meow! (Goodbye)"
End Sub
Public Property Get Legs() As Integer
Legs = legs_
End Property
Public Property Get Breed() As String
Breed = breed_
End Property
Public Property Get name() As String
name = name_
End Property
Public Property Let name(ByVal Value As String)
name_ = Value
End Property
Public Property Get Age() As Long
Age = age_
End Property
Public Property Let Age(ByVal Value As Long)
age_ = Value
End Property
Public Property Get Weight() As Double
Weight = weight_
End Property
Public Property Let Weight(ByVal Value As Double)
weight_ = Value
End Property
Public Property Get likes() As Collection
Set likes = likes_
End Property
Public Property Set likes(Value As Collection)
Set likes_ = Value
End Property
Public Property Get Talk() As String
Talk = meow_
End Property
Public Property Get Meow() As String
Meow = "The cat says: " & meow_
End Property
Public Property Let Talk(ByVal Value As String)
meow_ = Value
End Property
Public Property Get daysHoused() As Integer
daysHoused = Now() - arrivalDate_
End Property
' Implement required interface properties
Private Property Get Animal_Name() As String
Animal_Name = name
End Property
Private Property Get Animal_Age() As Long
Animal_Age = Age
End Property
Private Property Get Animal_Weight() As Double
Animal_Weight = Weight
End Property
Private Property Get Animal_Talk() As String
Animal_Talk = Meow
End Property
What I don't understand is when the code reaches this point:
Code:
Sub PrintCollection(coll As Collection, name As String)
Debug.Print vbNewLine & "Printing " & name & ":"
On Error Resume Next
Dim item As Variant
For Each item In coll
Debug.Print item.PrintOut() '*******************************
Next item
On Error GoTo 0
End Sub
after the line with ************************** is executed, the code jumps to the appropriate class.
How does it know which class to go to?