Alan2
Alan2

Reputation: 24572

How can I modify my class to override a list add?

I am using the following class that was suggested to me to track timings:

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;
  }
}

Here's my class that holds the list of times:

public abstract class BaseGridViewModel
{

    protected BaseGridViewModel()
    {
        Times = new List<TimeEvent>();
    }
    public IList<TimeEvent> Times {get; set;}

}

Here's how a start the stopwatch and add an event:

var sw = Stopwatch.StartNew();
vm.Times.Add(new TimeEvent(sw.ElapsedMillisends, "After event 1"));
sw.Stop();

Is there a way that I could make it so that I have a method inside my viewmodel that I could pass the values to and it would then do the same as the Add internally. For example a method like this:

vm.Event(sw, "After event 1"));

Even

Upvotes: 0

Views: 66

Answers (1)

armen.shimoon
armen.shimoon

Reputation: 6401

Why not?

public abstract class BaseGridViewModel
{

    protected BaseGridViewModel()
    {
        Times = new List<TimeEvent>();
    }
    public IList<TimeEvent> Times {get; set;}

    public void Event(StopWatch watch, string message)
    {
        Times.Add(new TimeEvent(watch.ElapsedMilliseconds, message));
    }

}

Upvotes: 5

Related Questions