Steffon Scott

Steffon Scott

  • NA
  • 20
  • 12.5k

Accessing a List object within the containing class-object.

Sep 23 2012 12:06 PM
        Hello again. I am continuing my experimental work on this text based game and using this as a hands-on learning and am having a little bit of trouble with the scope of a list defined within. 

        I have written a couple classes, with the help of some knowledgeable members here, to deal with the functionality I require. playerData.cs class is to hold information on individual players. playerFunctions.cs is used to define functions which can be performed on the list of players (playerList) as well as eventually functions to be performed on individual members(players) of the list as well. 

        Now, my problem is not that I am getting errors. Currently the rar that I uploaded does throw an error which I created while attempting to fix the scope problem I am dealing with. Now to be clear, my intention is not to simply fix the problem but to understand why it was caused so I can move on. I have read on scope declarations and how they work in several contexts, but am having trouble in mine. MY understanding is this, 

List<playerData> playerList = new List<playerData>();

is defined on line one of the playerFunctions class, which is in turn instantiated within the Main() function as, 

playerFunctions players = new playerFunctions();

--on the first line. Now, to my understand this makes players an instantiated object of playerFunction type which has members that I (should?) be able to access from the same class. Namely, 

this.playerList.Add(new playerData(name));

-- the ISSUE as a said though is not one of compile errors rather, when running the program I use the menu to add 2-3 players, then when I list it gives me nothing, no objects are listed AND the .Count of the playerList function is always 0. Which leads me to believe that the incorrect instance is being referenced OR somehow multiple instances of playerFunction are coming into play.

Sorry if my problem is vague, I'm trying to explain what is going on, alongside my understanding so that the answer will address my knowledgability shortfalls. Thanks in advance for you help.

PS: Also, if anyone has any tips on code structure I'd be happy to hear them, I'm sort of just organizing things how I think they should fit logically in my mind, but I don't know if that is the best way.

Attachment: game.rar

Answers (3)