简体   繁体   中英

Generic Types in VB.NET

The question is simple.

I have three generic structures like these:

The main structure

Public Structure Pointer(of T) 'Here is where the data type must be constrained
    Dim BP As BasePointer
    Dim BV As BaseValue(of T)

    'Some code
End Structure

The pointer structure

Public Structure BasePointer 'This is a pointer itself, no problem (I think)
    'Some code
End Structure

This structure takes the constrained data type by Pointer(of T)

Public Structure BaseValue(Of T)
'Some code
End Structure

As far as I know, structures can be initialized without the new constructor. The idea is constraint certain value types, but the thing is that they can be, for example, class (as type, not generic container), structure (also as type, not generic container), IntPtr, Char, Integer, float, delegate, enumeration, interface, etc. (as you can see, they are value and reference parameters, I can´t write (of T as class) or (of T as structure), because I don't know what type is going to be passed).

The idea of the program is to create a pointer with a main structure and two secondary structures. One of the secondary structures takes the value of the pointer and the other is a pointer itself.

The operation is as follows:

The main structure captures the type T (here is where I want to make the constraints), and, if the data type is valid, it passes it to the secondary structure that takes the already constrained value, and if not, it discards it (with that data type the pointer is not going to be created). Once the type is passed to the secondary value structure, the pointer is created.

Example:

Dim sample as Pointer(Of ULong)

If the ULong data type is within the constraints, the pointer will be passed to the BV variable (The variable initialized as BaseValue structure), otherwise a message will be displayed.

This, in a class, would have no problem, since it has to be initialized obligatorily. In a class, even if it has a generic type T without constraints, in the same class constructor I create the constraints and got it. But in a structure I don't know how to do it without having to initialize it.

I don't want to make a pointer to all the existing data types, only to those I consider more used (about 20, 25 or so, but of different data types). That's why the data restriction.

Previously I forgot to mention that if this can be done, you can write it in C# , which I more or less understand and I think I could translate it.

Again, thanks in advance.

That's not how generics work. The idea is that, inside a generic type or method, you can be sure that the generic type parameter has specific functionality. For instance, you could constrain T to implement the IComparable interface and then, inside your type or method, you know that you can access members of that interface on any T instance. You could constrain T to be a specific concrete type and then you know that any T instance will have the members of that type. A generic type constraint is basically saying that you are guaranteed to be able to use any T instance in a specific way. Saying that T can be any of a list of types and nothing outside that does NOT provide any guarantee of being able to use a T instance in a specific way so it is not applicable to generics.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM