Nearly there i think - Where am i going wrong with the class

mahmed1

Well-known Member
Joined
Mar 28, 2009
Messages
2,302
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi

I am trying to set up a class that has a Team name, Player Name and Players (Collection to add players) but i am getting an error saying variable not set. Please advise if im on the right track and give some pointers to where i can improve

Thank You

P.s how would i encorporate into the NEW class multiple names from the sheet1 Range A2:A10 where it hold the player names

Do i need to create an instance for each player name in the loop or can i leave it as 1 instance?

i.e

Code:
For i = 2 to 10

Set AddPlayer = New PlayersTeam.Player.Name = Sheets("Sheet1").Range("A" & i).value
Players.Add Player, Team.Player.Name
Set Players = Nothing
Next i

I have set up these 3 classes.

Team clas has 2 properties - Name and Player (class) to access Player Class

Normal Module

Code:
Sub test()
Dim Team As Team
Dim Player As Player
Dim Players As Players

Set Team = New Team
Set Player = New Player
Set Players= New Players

Team.Player.Name = "Joe Bloggs"
Players.Add Player, Team.Player.Name

End Sub

Team Class module

Code:
Private pName As String
Private pPlayer As Player
Public Property Let Name(value As String)
pName = value
End Property
Public Property Get Name() As String
Name = pName
End Property
Public Property Let Player(value As Player)
Set pPlayer = value
End Property
Public Property Get Player() As Player
Set Player = pPlayer
End Property

Player Class module

Code:
Private pName As String
Public Property Let Name(value As String)
pName = value
End Property
Public Property Get Name() As String
Name = pName
End Property

Players Class Module (Collection)

Code:
Private pAddPlayers As Collection

Private Sub Class_Initialize()
Set pAddPlayers = New Collection
End Sub

Sub Add(Item As Player, Key As String)
    pAddPlayers.Add Item, Key
End Sub

Public Property Get Count() As Long
Count = pAddPlayers.Count
End Property

Public Property Get Item(NameORnumber As Variant) As Player
Set Item = pAddPlayers.Item(NameORnumber)
End Property
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
For some reason its getting printed all weird

Hi Kyle - Once again Thank you so much. I think I now have a really good understanding in regards to class modules thanks to yourself and I am practicing to get more used to it
I did not know that you could pass a collection to another collection
From my understanding, would I be correct in saying the way this code is working is something like this
Code:
Public Function AddTeam(TeamName As String) As Team
If Not cTeams.Exists(TeamName) Then
Dim t As Team
Set t = New Team
t.Name = TeamName
cTeams.Add TeamName, t
End If
Set AddTeam = cTeams(TeamName)
End Function

Public Function AddTeamAndPlayer(TeamName As String, PlayerName As String) As Player
Set AddTeamAndPlayer = AddTeam(TeamName).AddPlayer(PlayerName)
End Function

Public Function AddTeamAndPlayer(TeamName As String, PlayerName As String) As Player
'Set AddTeamAndPlayer = Whatever is in Addteam and then whatever is within that (Like a nested if FUNCTION)
Set AddTeamAndPlayer = AddTeam(TeamName).AddPlayer(PlayerName)
End Function
So by Setting AddTeam by using the AddTeam function is saying

Set AddTeam to collection("Boys Team")
so now this line becomes collection Boys Team
Code:
Set AddTeamAndPlayer = AddTeam(Collection "Boys Team") 
.AddPlayer(PlayerName)

and then it processes the next part
Code:
.AddPlayer(PlayerName)

and by setting the player to the AddTeam its setting AddPlayer to the collection which holds that player details.
P.s why is the SET and AS PLAYER needed in the AddTeamandPlayer Function as I cant see how this is being used again in the main code so could I have not done

Code:
Public Function AddTeamAndPlayer(TeamName As String, PlayerName As String)
AddTeamAndPlayer = AddTeam(TeamName).AddPlayer(PlayerName)
End Function
 
Last edited by a moderator:
Upvote 0
No, that's not how it works. We aren't passing about a collection/dictionary, we are passing an element of that collection which is either an object of type Team or a Player object.
The syntax for accessing an element from a collection or dictionary is as follows:
Code:
[Set] myVariable = Collection(Key) 'Set required for objects
Or as in the above
Code:
Set AddTeam = cTeams(TeamName)

So this line:
Code:
Set AddTeamAndPlayer = AddTeam(TeamName).AddPlayer(PlayerName)
Expanded out would be written like this:
Code:
Dim myTeam as Team
Dim myPlayer as Player

set myTeam = AddTeam(TeamName) 
set myPlayer = myTeam.AddPlayer(PlayerName)
set AddTeamAndPlayer = myPlayer

As this should demonstrate, we are just referencing objects within the collections, not the collections themselves

P.s why is the SET and AS PLAYER needed in the AddTeamandPlayer Function as I cant see how this is being used again in the main code so could I have not done
Public Function AddTeamAndPlayer(TeamName As String, PlayerName As String)
AddTeamAndPlayer = AddTeam(TeamName).AddPlayer(PlayerName)
End Function

<tbody>
</tbody>

No, you couldn't; whilst you do not actually access the returned player in the calling code, the option is there should you wish to do so. If you wanted to remove this functionality, you wouldn't use a function since you wouldn't want to return anything - you'd use a sub:
Code:
Public Sub AddTeamAndPlayer(TeamName As String, PlayerName As String)
    AddTeam(TeamName).AddPlayer PlayerName
End Sub
 
Last edited:
Upvote 0
Hi Kyle

firstly i want to thank you again

you truly are a MVP and i would recommend again that you are worthy of that title

is there any chance you could based on my example give an example where i could use this interface structure as i will have both a manager and agent who are employees but have same properties?

many thanks
 
Upvote 0
Post a new thread, it's not really related to this and this one is getting a bit long ;)
 
Upvote 0

Forum statistics

Threads
1,216,411
Messages
6,130,440
Members
449,581
Latest member
econtent2

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