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
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.
VulpesPosted Sep 23, 2012, 12:54 PM
In program.cs:
In playerFunctions.cs:
In Menu.cs:
You don't need to create a playerFunctions object in the Main method as this is being done in the menu object.
You only need to pass the name to the addChar() method to add a new character.
However, the main point here is that 'players' needs to be a field in the menu class, not a local variable of the ShowMenu() method.
A local variable is destroyed when the method returns and, worse still, you're creating a new local variable (and hence a new playerFunctions object) each time the ShowMenu() method calls itself recursively. This is why you can never list the players because the current players object is empty - it's the earlier objects which contain the players and these are no longer accessible.
By making players into a field, the menu object only contains a single playerFunctions object and all methods (including recursively called methods) have access to it.
VulpesPosted Sep 23, 2012, 3:32 PM
Steffon ScottPosted Sep 23, 2012, 1:41 PM