.NET - Custom Types Possible?

c#, delphi, types, vb.net

Solution

How about defining custom classes that derive from the closed type of your generic collection? For example:

public class MyType : ObservableCollection<MyClass>
{ }

Then you could create another generic collection whose type parameter is your above-defined class (which is itself a collection):

ObservableCollection<MyType> lotsOfData = new ObservableCollection<MyType>();

When you iterate over it, you would get the sequence of inner collections:

foreach (ObservableCollection<MyClass> data in lotsOfData)
{
    DoSomething(data);
}

Edit: Deriving as shown above will allow you to inherit all accessible members from the base `ObservableCollection<T>` class, but you won’t be able to call its (non-default) constructors. Thus, you would typically want to implement the constructors with the same overloads as the base class:

public class MyType : ObservableCollection<MyClass>
{
    public MyType()
        : base()
    { }

    public MyType(IEnumerable<MyClass> collection)
        : base(collection)
    { }

    public MyType(List<MyClass> list)
        : base(list)
    { }
}

Problem

In Delphi you can do something like this : ``` TArray = array[1..3] of byte; ``` where you can then declare ``` T2Array = array[1..3] of TArray ``` ad nauseum... Does something like this exist in .NET? (vb, c#, whatever) I am currently doing something like this ``` Private LotsOfData As ObservableCollection(Of ObservableCollection(Of myClass)) ``` but would like to do ``` Private LotsOfData As ObservableCollection(Of myType) ``` where ``` myType --> ObservableCollection(Of myClass) ``` I know you can do this with structures, ie: ``` Public Structure MyType Public myOc as ObservableCollection(Of MyClass) End Structure Dim LotsOfData as ObservableCollection(of MyType) ``` but you then have to reference it as (for example) ``` LotsOfData.Last.myOc(i) ``` instead of ``` LotsOfData.Last(i) ``` which seems clumsy. This also seems clumsy : ``` For Each Data as ObservableCollection(of myClass) in LotsOfData DoSomething(Data) Next ``` as does ``` For Each Data as MyType in LotsOfData DoSomething(Data.myOc) Next ``` when it could be ``` For Each Data as MyType in LotsOfData DoSomething(Data) Next ``` Any ideas?

Original source