Object in Array Creation Problem

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:

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
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Code:
'Change
Set oGame.HomeTeam = aoTeam(lngGame)
'To
oGame.HomeTeam = aoTeam(lngGame)

The Set happens in clsTeam:
Code:
Public Property Let HomeTeam(ByVal HomeTeam As clsTeam)
    [COLOR=#ff0000][B]Set [/B][/COLOR]pHomeTeam = HomeTeam
End Property
 
Upvote 0
Or if you want to use Set in the calling code, change the Property:
Rich (BB code):
Public Property Set HomeTeam(ByVal HomeTeam As clsTeam)
    Set pHomeTeam = HomeTeam
End Property
 
Upvote 0
Thanks very much for posting these. I know it seems basic but it's taken me a long time to grasp classes. I'm determined to crack it and with help like yours I surely will. It worked!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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