views:

284

answers:

6

I have a class Similar to this

 public class Model
{
    public TimeSpan Time1 {get; set;}
    public TimeSpan Time2 { get; set; }
    public TimeSpan Time3 { get; set; }
    public TimeSpan Time4 { get; set; }

}

Now Let's Imagine I have to populate the times during runtime and then Figure out the time remaining between Time 1 and Time 2, then when that passes Find the time remaining between Time2 and Time3 and so on. However, I need to take into account what the time is right now.

For Example:

Now it is 1:00 PM

Time1=5:00 AM Time 2 = 12:00 PM Time 3= 4:00 PM Time 4 = 6:00 PM

So since the time is 1:00PM, I need to find the difference between Time 2 and Time 3

Now is there a smarter way other than reflection to determine this? Should i add something in my class

+3  A: 

Why would you not use an array or list?

public class Model
{
    public List<DateTime> Dates { get; set; }
}
Dean Harding
Well I have a Display Attribute on top of the 4 since they are unique. I just simplified the idea to get my point across.
A: 

You could add a property or method that would return the times as a list for easier processing. Then your external code could access the times as either a list or as individual properties. You still have to explicitly access each property, but at least it's from within the same class, so you're not coupling your external code to the data structure as tightly.

CodeSavvyGeek
+5  A: 

If you need to keep the existing structure of your class, you could add a method to enumerate through the times:

public class Model
{
    public TimeSpan Time1 {get; set;}
    public TimeSpan Time2 { get; set; }
    public TimeSpan Time3 { get; set; }
    public TimeSpan Time4 { get; set; }

    public IEnumerable<TimeSpan> GetTimes()
    {
        yield return Time1;
        yield return Time2;
        yield return Time3;
        yield return Time4;
    }
}

And use it like this:

foreach (TimeSpan time in model.GetTimes())
{
    // use the TimeSpan
}
fre0n
A: 

If I'm understanding your question correctly, why not have a Dictionary<TimeSpan, string> where the key is actual TimeSpan, and the value is the name, e.g. "Time1"? This way, you can sort the keys, find the pair that you need, and then get at their names.

A: 

This is untested and needs some optimization, but just as a thought example, use an array and iterate through it.

public class Model
{
    private TimeSpan[] _timeSpans = new TimeSpan[4] { new TimeSpan(), new TimeSpan(), new TimeSpan(), new TimeSpan() };

    public TimeSpan Time1
    {
        get { return _timeSpans[0]; }
        set { _timeSpans[0] = value; }
    }
    public TimeSpan Time2
    {
        get { return _timeSpans[1]; }
        set { _timeSpans[1] = value; }
    }
    public TimeSpan Time3
    {
        get { return _timeSpans[2]; }
        set { _timeSpans[2] = value; }
    }
    public TimeSpan Time4
    {
        get { return _timeSpans[3]; }
        set { _timeSpans[3] = value; }
    }

    // DateTime.TimeOfDay holds the time portion of a time
    public TimeSpan GetDifference(TimeSpan currentTime)
    {
        int start = -1;
        for(int i = 0; i<_timeSpans.Length;i++)
        {
            if(_timeSpans[i] >= currentTime)
            {
                start = i;
                break;
            }
        }
        if(start == -1) throw new ArgumentException("handle the case where currentTime is smaller than all of them");
        int end = (start + 1 < _timeSpans.Length) ? start + 1 : 0;
        return _timeSpans[end] - _timeSpans[start];
    }
Michael Stum
A: 

Constructing an array provides for a simple linq statement to calculate the time span:

public class Model
{
    public TimeSpan Time1 { get; set; }
    public TimeSpan Time2 { get; set; }
    public TimeSpan Time3 { get; set; }
    public TimeSpan Time4 { get; set; }

    public TimeSpan GetSpan(TimeSpan time)
    {
        var times = new[] { Time1, Time2, Time3, Time4 };

        return Enumerable.Range(1, times.Length - 1)
            .Select(i => new[] { times[i - 1], times[i] })
            .Where(t => t[0] <= time && time < t[1])
            .Select(t => t[1] - t[0])
            .FirstOrDefault();
    }
}
tames