views:

1039

answers:

5

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.

+9  A: 

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
}
Lasse V. Karlsen
+1, You type insanely fast.
Brandon
I have a high-speed network connection to my brain. I don't type at all.
Lasse V. Karlsen
+1 You must have had that answer ready before the question came ;o)
Fredrik Mörk
...and I *love* that single code comment!
Fredrik Mörk
Please don't encourage the use of mutable structs. They're bugs waiting to happen. (I'd say ditto public fields in general, but definitely mutable structs.)
Jon Skeet
Ok, I'll agree on that one. Will edit.
Lasse V. Karlsen
All good, except I'd not use `struct` here for a mutable type (for reasons that are well explained in any decent book on C#).
Pavel Minaev
I changed struct to class. I was trying to make a simple answer, and fell into the trap of simplifying too much.
Lasse V. Karlsen
Where can I order one of them brain interfaces? Goodbye RSI! \o/
Thorarin
I'm sorry, I don't know, I was manuf^H^H^H^H^H born with that.
Lasse V. Karlsen
Personally, I'd make a constructor. But C# 3 has that with named properties, doesn't it? Stuck in yesteryear.. ah..
maxwellb
Yeah, but in my opinion, this question smells like homework, so I was trying to keep it at a level that wouldn't drag in every odd syntax (though useful) from C#. Sure, I could add a constructor and/or use initialization syntax, but I'll concede that a mutable struct was easily fixable, but I'll leave the rest so that the code is understandable.
Lasse V. Karlsen
+3  A: 

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);
}
Lucas Jones
Right, that is pretty much word for word what I was still working on typing out ;)
Thorarin
+1 for: since you're using C# 3 language features, such as automatic properties, might as well use the named property constructor.
maxwellb
A: 

I think you will have to be a bit more specific as to what you are trying to do. Generally, you will need a data structure to hold your Date, String, Boolean, and Integer. You can use a Struct or a custom Class to do this. You can also use an anonymous type, but this is an advanced concept that you probably want to stay away from.

So, to add one of these elements to your collection, you just do:

myCollection.Add(myNewLogElement);

To loop through this collection, you do:

foreach (MyLogElementType logElement in myCollection
{
   ' do something...
}

I hope this helps. If you need a better answer, you will need to be more specific as to what you are doing.

Good luck.

Mike Gates
+1  A: 

I'll start by saying that the object oriented answers are better...but you did ask for the simplest solution.

First make a new collection:

List<string> log = new List<string>();

When you want log something:

log.Add( string.Format( "Date = {0}, String = {1}, Boolean = {3}, and an integer = {4}", theDate, theString, theBool, theInt );

Then at the end:

foreach( string logEntry in log ) Console.WriteLine( logEntry );
Greg
A: 

Exploiting more of 3.0.

public class GameRound{
    public DateTime Date { get; set; }
    public String Name { get; set; }
    public Boolean DidCheat { get; set; }

    public void Print()
    {
        Console.WriteLine(String.Format("Date:{0}, Name:{1}, Cheated: {2}", Date, Name, DidCheat));
    }
}

public class GameRounds : List<GameRound>{
   public void Add(DateTime date, string name, bool didCheat) {
                this.Add(new GameRound { Date = date, Name = name, DidCheat = didCheat });
   }
}

public static void Play(){
            var gameRounds = new GameRounds();
            //Round 1
            gameRounds.Add(DateTime.Today, "ABC", false);
            //Round 2
            gameRounds.Add(DateTime.Today, "DEF", true);

            //Print
            foreach (var gameRound in gameRounds)
            {
                gameRound.Print();
            }
 }
Amby