3

I have the following class:

public abstract class BaseGridViewModel
{
    protected BaseGridViewModel()
    {
        Timer = new List<long>();
    }
    public List<long> Timer { get; set; }
}

To update the timer I have:

vm.Timer.Add(sw.ElapsedMilliseconds);

I am not so familiar with lists. Is it possible for me to have a list that contains both a long and a string so that I could do the following:

vm.Timer.Add(sw.ElapsedMilliseconds, "After event 1");
vm.Timer.Add(sw.ElapsedMilliseconds, "After event 2");
Alan2
  • 23,493
  • 79
  • 256
  • 450
  • 1
    One way would be to use a `List>` instead. – Tim Schmelter Sep 12 '12 at 22:33
  • @TimSchmelter - Isn't a Tuple immutable though? That would prevent the OP from updating values if they wanted/needed to. – Tim Sep 12 '12 at 22:35
  • 2
    @Tim Without alot of context, I would think this is a perfect place for an immutable type. – asawyer Sep 12 '12 at 22:37
  • I would not need to update the values once they had been set. Could you possibly give me an example of how I could use this. Thanks – Alan2 Sep 12 '12 at 22:40
  • @Gemma http://stackoverflow.com/questions/5097287/how-to-create-immutable-objects-in-c – asawyer Sep 12 '12 at 22:44
  • @Gemma: have a look at [MSDN](http://msdn.microsoft.com/en-us/library/dd268536.aspx). Just create a `List>` and add it in this way: `timers.Add(Tuple.Create(sw.ElapsedMilliseconds, "After event 1"));`. You get these values via `Item1` and `Item2` properties. – Tim Schmelter Sep 12 '12 at 22:47

2 Answers2

7

Create a class to hold the two values, and store them in the list instead.

public sealed class TimeEvent
{
  public long Elapsed {get; private set;}
  public string Description { get; private set;}
  public TimeEvent(long elapsedTime, string descriptionOfEvent)
  {
    this.Elapsed = elapsedTime;
    this.Description = descriptionOfEvent;
  }
}

public IList<TimeEvent> TimerEvents {get; set;}

vm.Timer.Add(new TimeEvent(sw.ElapsedMillisends, "After event 1"));
asawyer
  • 17,642
  • 8
  • 59
  • 87
  • What do you think about the idea of using a Tuple as suggested in the comments? – Alan2 Sep 12 '12 at 22:41
  • 3
    @Gemma Tuples are fine in some cases but generally I prefer a class with named meaningful properties rather then Item1, Item2, ect. – asawyer Sep 12 '12 at 22:42
  • And if you wanted to get *really* crazy you could even implement a collection class (e.g., `Timeline`) with a custom `Add` method plus some convenience methods/properties like `StartTime`, `EndTime`, `TotalElapsed`, etc. – Dan Tao Sep 12 '12 at 23:02
  • @DanTao Add in some automatic database and xml serialization stuff and you have the tool I wrote to gather database performance metrics :) – asawyer Sep 12 '12 at 23:03
2

You can create a class and then create a List of that class.

public class TimerData
{
    public long time;
    public string description;

    public TimerData(long t, string d)
    {
        this.time = t;
        this.description = d;
    }
} 


List<EventData> list = new List<EventData>();
list.Add(new TimerData(50, "Description"));

There are more advanced things you could do with your class, but this method is simple.

Chimera
  • 5,884
  • 7
  • 49
  • 81