J...
J...

Reputation: 31443

.NET - Custom Types Possible?

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?

Upvotes: 6

Views: 12989

Answers (4)

Rob Kennedy
Rob Kennedy

Reputation: 163347

You can define type aliases in Oxygene, the .Net Delphi language. Same syntax as classic Delphi.

type
  MyType = ObservableCollection<MyClass>;

You can use public to make the name visible in other assemblies.

Upvotes: 2

Douglas
Douglas

Reputation: 54897

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)
    { }
}

Upvotes: 5

JaredPar
JaredPar

Reputation: 755209

It looks like you're trying to create a type alias in the code. VB.Net does have support for that in the form of the Imports directive

Imports myType = System.Collections.Generic.ObservableCollection(Of myClass)

...

Private LotsOfData As ObservableCollection(Of myType)

Note the Imports directive must be repeated for every file in which you want to use myType.

Upvotes: 4

ctorx
ctorx

Reputation: 6941

Start with IEnumerable where T is the type you are operating on. There are many implementations including List<T> which allows you to easily sort, push and pop elements.

For example...

var myListOfThings = new List<Thing>(); 
myListOfThings.Add(new Thing {Name = "Green thing" }); 
myListOfThings.Add(new Thing { Name = "Red thing" }); 
myListOfThings.Add(new Thing { Name = "Yellow thing" });

And when using IEnumerable you get access to LINQ...

var redThing = myListOfThings.FirstOrDefault(x => x.Name == "Red Thing");

And easy iteration...

foreach(var thing in myListOfThings)
{
     // do something
}

NOTE: I don't have any VB examples, but you tagged the question with C#, so I hope these are useful.

Upvotes: 1

Related Questions