nigelandrewfoster
Well-known Member
- Joined
- May 27, 2009
- Messages
- 747
Hello, I'm experimenting with a more object oriented approach. The code below is supposed to create a set of objects for match fixtures. aoTeam() stores an array of team objects without problem. A game object has two objects attached - .HomeTeam and .AwayTeam. When I come to set the home team, I get the Object Variable Not Set error (highlighted below). Why? Thanks for looking.
Here's the clsGame code:
And here's the code to create the objects:
Thanks
Here's the clsGame code:
Code:
Option Explicit
Private pGameName As String
Private pHomeTeam As clsTeam
Private pAwayTeam As clsTeam
Private pHomeGoals As Integer
Private pAwayGoals As Integer
Public Property Get Name() As String
Name = pGameName
End Property
Public Property Let Name(ByVal sName As String)
pGameName = sName
End Property
Public Property Get HomeTeam() As clsTeam
Set HomeTeam = pHomeTeam
End Property
Public Property Let HomeTeam(ByVal HomeTeam As clsTeam)
Set pHomeTeam = HomeTeam
End Property
Public Property Get AwayTeam() As clsTeam
Set AwayTeam = pAwayTeam
End Property
Public Property Let AwayTeam(ByVal AwayTeam As clsTeam)
Set pAwayTeam = AwayTeam
End Property
And here's the code to create the objects:
Code:
Sub Create_Objects(lngNoOfTeams As Long)
Dim Rounds As Collection
Dim Fixtures As Collection
Dim TeamGames As Collection
Dim Teams As Collection
Dim Games As Collection
Dim oRound As clsRound
Dim oGame As clsGame
Dim oTeam As clsTeam
Dim aoTeam() As Object
Dim lngNoOfRounds As Long
Dim lngRound As Long
Dim lngNoOfGames As Long
Dim lngGame As Long
Dim lngGameCount As Long
Dim lngTeam As Long
Dim blnTeamsEven As Boolean
ReDim aoTeam(lngNoOfTeams)
Set Rounds = New Collection
Set Teams = New Collection
blnTeamsEven = (lngNoOfTeams / 2 = Int(lngNoOfTeams / 2))
lngNoOfRounds = lngNoOfTeams + blnTeamsEven
lngNoOfGames = (lngNoOfTeams - blnTeamsEven - 1) / 2
For lngTeam = 1 To lngNoOfTeams
Set oTeam = New clsTeam
Teams.Add oTeam
Set aoTeam(lngTeam) = oTeam
oTeam.Name = "Team " & lngTeam
Range("Team_Names").Offset(lngTeam).Value = oTeam.Name
Next
Set TeamGames = New Collection
For lngRound = 1 To lngNoOfRounds
Set oRound = New clsRound
Set Games = New Collection
Rounds.Add oRound
oRound.RoundName = "Round " & lngRound
For lngGame = 2 + blnTeamsEven To lngNoOfGames + blnTeamsEven + 1
Set oGame = New clsGame
Games.Add oGame
oGame.Name = "Game " & lngGame
[I][B] Set oGame.HomeTeam = aoTeam(lngGame)[/B][/I]
Set oGame.AwayTeam = aoTeam(lngNoOfTeams - lngGame + 2 + blnTeamsEven)
With Range("Fixtures").Offset(lngGameCount)
.Value = lngRound
.Offset(, 1).Value = oGame.HomeTeam.Name
.Offset(, 2).Value = oGame.AwayTeam.Name
End With
lngGameCount = lngGameCount + 1
Next lngGame
Set oRound.GameAdd = Games
Next lngRound
Thanks