.NET - Are Custom Types Possible?

In Delphi, you can do something like this:

TArray = array[1..3] of byte;

      

where can you 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 it with structs, that is:

Public Structure MyType
     Public myOc as ObservableCollection(Of MyClass)
End Structure

Dim LotsOfData as ObservableCollection(of MyType)

      

but you have to refer to it like (for example)

LotsOfData.Last.myOc(i) 

      

instead

LotsOfData.Last(i)

      

which seems awkward. This also seems awkward:

For Each Data as ObservableCollection(of myClass) in LotsOfData
     DoSomething(Data)
Next

      

as well

For Each Data as MyType in LotsOfData
     DoSomething(Data.myOc)
Next

      

when can it be

For Each Data as MyType in LotsOfData
     DoSomething(Data)
Next

      

Any ideas?

+3


source to share


4 answers


How do I define custom classes that come from the private type of your shared collection? For example:

public class MyType : ObservableCollection<MyClass>
{ }

      

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

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

      



When you iterate over it, you end up with a sequence of internal collections:

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

      

Edit . The output as shown above will allow you to inherit all available members from the base class ObservableCollection<T>

, but you will not be able to call its (non-standard) constructors. Thus, you would usually like to implement 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)
    { }
}

      

+4


source


It looks like you are trying to create a type alias in your code. VB.Net has support for this in the form of a directiveImports

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

...

Private LotsOfData As ObservableCollection(Of myType)

      



Please note that the directive Imports

must be repeated for every file you want to use myType

.

+4


source


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

type
  MyType = ObservableCollection<MyClass>;

      

You can use public

to make the name visible in other assemblies.

+2


source


Start with IEnumerable, where T is the type you are working on. There are many implementations, including List<T>

one that allows you to easily sort, click and pop items.

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 a simple iteration ...

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

      

NOTE. I don't have VB examples, but you tagged the question with C #, so I hope this is helpful.

+1


source







All Articles