Generic Types
This's my class
Class CClass
Public Sub New(TheTypeShouldBeAS ? ? ? ? What ? ? ? ?)
' So That i can define a generict List of 'ThisShouldBeTheType'
Dim myList as NewList(Of TheTypeShouldBe)
End Sub
End Class
Many Thanks
This's my class
Class CClass
Public Sub New(TheTypeShouldBeAS ? ? ? ? What ? ? ? ?)
' So That i can define a generict List of 'ThisShouldBeTheType'
Dim myList as NewList(Of TheTypeShouldBe)
End Sub
End Class
Many Thanks
This is probably want you want:
Public Class MyList(Of T) Private _List As List(Of T) End Class |
Then to create it, simply do the following:
Dim myList As New MyList(Of String) |
- Is it possible to limit the types of T (say only to integer and string)
- Is it possible to to auto call functions based on the specific type, and the compiler auto know what to use on that type? (sorry confused here) like if there are two functions, one for integer, on for string, how can i specify which to use, based on the type.
Many Regards.
There is no way at compile to limit 'T' to only strings and integers. It has to be done a runtime.
However you can limit types to a certain interface or base class, or only reference types, or only value types. As far on the called methods on the object instance, these calls are limited to what the compiler knows about the type.
For example, from above, you would only be able to call methods exist on Object as that is the only common type that T derives from. However, if you did the following:
Public Class MyList(Of T As IMyInterface) Private _List As List(Of T)
End Class Public Interface IMyInterface Sub MyMethod() End Interface |
Basically what the above is doing, is limiting T to types that implement IMyInterface. And because the compiler knows that IMyInterface has a method MyMethod and that T must be a IMyInterface, it can call MyMethod on the object.
To limit T to only reference types (classes), do the following:
Public Class MyList(Of T As Class) End Class |
To limit T to only value types (structures), do the following:
Public Class MyList(Of T As Structure) End Class |
Now back to your first question, to limit to strings and integers you would need to do a runtime check in the static constructor, like so:
Public Class MyList(Of T) Shared Sub New() |
This would only execute at runtime, so the user won't be able to know they have done the wrong thing until their code runs.
Hope that helps.
David
Many Thanks. This was really helpful.
As for the second part of:
"Is it possible to to auto call functions based on the specific type, and the compiler auto know what to use on that type? (sorry confused here) like if there are two functions, one for integer, on for string, how can i specify which to use, based on the type. "
I am thinking that:
the use of some overloaded functions should do the trick. Not Sure Yet. (Especially if i have two functions, one taking Object and one taking ListBox, now listbox is also an object, which function would compiler call, is it random, or would it call the listbox one always)
Mnay Thanks Again.
Hi, and overloaded functions DO NOT Work
Please would anyone know how to let the Generic Class know which function to call. say i have in the generic class on function that operates on integer and one that operates on strings.
Now how do i specify if the type of T is string or Integer so the appropriate function i called. (i am currently using gettype, and matching for each item i an array, but that is a big overhead)
Thanks.