0

I want to have a time and value class, where the value might be any type, and then put that into some kind of collection.

public class TimeValue<T> {
    public DateTime TimeStamp { get; set; } = DateTime.Now;
    public T Value { get; set; } = default(T);

    TimeValue() { } // constructors
    TimeValue(DateTime ts, T val) { TimeStamp = ts; Value = val; }
    TimeValue(TimeValue<T> tv) { TimeStamp = tv.TimeStamp; Value = tv.Value; }  
}

This oldish answer Collection of generic types referred to by Different Generics T in the same collection requires putting Type within the class and use of a Dictionary and I don't want to mix types within the collection as the referring question does.

I'd like to specify collection the types (as below), which only specifies the type for the Value. Then the collection will be of essentially TimeValue< typeof(Value)> entries:

TimeValueCollection<double> timeDoubles;
TimeValueCollection<int> timeInts;

After Jonny's comment, this is what I have. Not sure how IEquatable<TimeValue<T>> would work or which collection will suit the need best.

public class TimeValueCollection<T> : ICollection<TimeValue<T>> {
    ICollection<TimeValue<T>> items;

    public TimeValueCollection() => items = new List<TimeValue<T>>();
    // todo:
    // properties matching collection chosen.
    // methods matching collection chosen
}

With Jonny's second comment, I can't see any benefit in the construct I was pursuing over this:

public class TimeValueCollection<DateTime,T> : IDictionary<DateTime,T>
{
    IDictionary<DateTime, T> items;
    public TimeValueCollection() => items = new Dictionary<DateTime, T>();
    private Type typeName = typeof(T);
    public Type ValueType { get => typeName; }
    public T this[DateTime key] { get => items[key]; set => items[key] = value; }
    // todo: remaining properties and methods... 
}
codebender
  • 469
  • 1
  • 6
  • 23
  • Make a collection of type "object", then use typeof on an element when you want to know an element's type. – Digiproc Jan 20 '19 at 01:34
  • Its unclear what you actually want – TheGeneral Jan 20 '19 at 01:36
  • So you want each collection to have one specific type (i.e. `int`)? or you want one collection to have multiple types at same time? I try your code and it is working as supposed I only done minor changes: 1) Make sure to initialize `_items` correctly. 2) Implement required `ICollection` functions. Please clarify what is not working exactly so we can help you – Ebraheem Jan 20 '19 at 15:00
  • 2
    You don't need to complicate `TimeValueCollection` just change `_items` to `List>` that will lead you to the solution. One question, if your `TimeValue` implements `IEquatable>` how it would look like? – Johnny Jan 20 '19 at 20:38
  • 1
    I am asking it because the only logical equality, with the current implementation, of the `TimeValue` would be to check only `Value` equality but not `TimeStamp`. Otherwise, it will be really hard\unclear how to implement `Contains` or `Remove` methods of the `ICollection` – Johnny Jan 20 '19 at 21:10

0 Answers0