Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.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?

like image 212
J... Avatar asked Apr 05 '12 20:04

J...


People also ask

What are custom types in C#?

All the C# built-in types, such as int, bool, etc. are value types, except string and object, which are reference types. You can also create your own value types using custom value types. There are two types of custom value types in C#: structs and enums.

What keyword defines a custom value type?

C# allows us to define custom value types by using struct and enum keywords.

What is Typeof class C#?

The C# typeof operator ( GetType operator in Visual Basic) is used to get a Type object representing String. From this Type object, the GetMethod method is used to get a MethodInfo representing the String. Substring overload that takes a starting location and a length.

What is net type?

NET type is a collection of members, which may be fields (i.e., they hold data of some type), methods (i.e., they contain code), or nested type definitions, and all members have some level of protection (e.g., public, private, protected).


1 Answers

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)
    { }
}
like image 70
Douglas Avatar answered Oct 19 '22 22:10

Douglas