I'm stuck and I need help. Anyway I have Visual Studio 2008 console program that needs to log a Date, String, Boolean, and an integer for each round of a game. Say the user plays 10 rounds I would have 10 of each.
I want to save those records in a data structure (collection i believe) and then after the game is finished loop through them producing a simple console report.
I'm stuck. It seems like a simple thing, but I can't find the solution. I imagine that there are multiple ways to accomplish this, but I'm looking for the most simple solution.
Edit: I wanted to simplify the answer to this, but perhaps I simplified too much. I've changed the mutable struct below (which is a bad idea, thanks for reminding me @Jon Skeet), with a class, and properties as well.
First, create the data type to hold the information:
public class RoundInformation
{
public DateTime Date { get; set; }
public String Name { get; set; }
public Boolean DidCheat { get; set; }
}
Then create the list to hold it:
public List<RoundInformation> Rounds = new List<RoundInformation>();
Then, for each round, construct a value with the round information data and add it to the list:
RoundInformation info = new RoundInformation();
info.Date = DateTime.Now;
info.Name = "Bob";
info.DidCheat = true; // Bob always cheats
Rounds.Add(info);
To loop through:
foreach (RoundInformation round in Rounds)
{
.. dump to console, left as an excercise
}
Try this:
public class GameRound {
public DateTime Date {get; set;}
public string String {get; set;}
public bool Boolean { get; set;}
public int Integer { get; set; }
}
In one part, with the correct variable names.
Then, in your console program, add the following line at the top:
List<GameRound> rounds = new List<GameRound>();
This makes a "list" of rounds, which can be added to, removed, and "looped" through.
Then, when you want to add a round, use code like this:
rounds.Add(new GameRound { Date = theDate, String = theString, Boolean = theBool, Integer = theInt });
This makes a new GameRound object, and sets all the properties to values. Remember to substitute theDate, etc. for the correct names/values.
Finally, to produce the report, try this:
foreach ( GameRound round in rounds ) {
Console.WriteLine("Date: {0}\nString: {1}\nBoolean: {2}\nInteger: {3}", round.Date, round.String, round.Boolean, round.Integer);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With