Click here to Skip to main content
14,930,311 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have 1 static list consisting of objects which then hold another static list of objects. I need to access one particular variable within one of the list elements of the second list within a particular element in the first list. The first list comes from the class "game" and the second list from the same class im writing in (class Player). Currently code looks like this and I'm just not sure how to write this code snippet. I've googled a lot and can't find an answer. Here's my code so far:

int calcpoints = game.Playerlist[turnloop].playerturn[playerturn.Count].Throwscore1


Playerlist is first list within class game
playerturn is second list within current class
turnloop is simply an int variable, not relevant
throwscore1 is variable within object of second list.

What I have tried:

I've tried:

int calcpoints = game.Playerlist[turnloop]Player.playerturn[playerturn.Count].Throwscore1


and:

int calcpoints = game.Playerlist[turnloop].(playerturn[playerturn.Count]).Throwscore1


neither worked particularly well, although second one gave me error "method name expected" even though Playerlist is not part of a method.
Posted
Updated 28-Mar-21 20:39pm

1 solution

Stop guessing, and thing about your data structures.
So the Game class contains a List of players: get it into a variable:
C#
List<Player> players = game.PlayerList;
Now us that to retrieve the specific play you ar einterested in:
C#
Player player = players[turnloop];

Your Player contains a list of ... no idea what ... :
C#
List<IDunno> theList = player.playerturn;
Now fetch whatever it is from that:
C#
IDunno whatEver = theList[playerturn.Count - 1];

Then do whatever it is you have to with that!
   
Comments
Borkanders bork 29-Mar-21 11:05am
   
I suppose I forgot to mention but the second list contains objects of a third class "Turns". This has caused an exception when I applied your code. The fourth and last line of code throws the "System.NullReferenceException: 'Object reference not set to an instance of an object." exception. Any ideas on how to fix this?
OriginalGriff 29-Mar-21 11:31am
   
So change the code to fit the data structures you created - I have no idea what they are, remember?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900