Urs,
| Public Function ToArray(Of T)(vList as IList) As T()
| return directcast(vList.ToArray(T),T())
| End Function
I have not tried (I'm in the middle of rebuilding my test machine).
I'm not sure if VB and/or the Framework will allow the DirectCast or not.
There are a number of rules on what is allowed & what is not allowed, I
don't remember them all...
I would mostly expect it would be allowed.
However! As you may know with the new Generic list classes your conversion
is "unneeded". ;-)
| > NOTE: VB is able to figure out the types, so IIf(Of Integer) is not
| > specifically needed...
| is that new? 2003 doesn't do it
Yes its new as Generics are new... it is closely related to overloading
methods. In C# its called Type Inference & is explained here, I don't see
the VB definition.
http://msdn2.microsoft.com/library/t...us,vs.80).aspx
Here is a VB swap sample (based on the above):
Public Module UtilityModule
Public Sub Swap(Of T)(ByRef lhs As T, ByRef rhs As T)
Dim temp As T
temp = lhs
lhs = rhs
rhs = temp
End Sub
End Module
Then you can call Swap with either:
Dim a As Integer = 1
Dim b As Integer = 2
Swap(Of Integer)(a, b) ' explicitly call Swap(Of Integer)
Swap(a, b) ' implicitly call Swap(Of Integer)
'as Integer was inferred from the parameters.
| Boah, is somebody still using Choose? :-)
I use it where it makes sense to use it! It makes sense to use it where you
have a list of values to choose from. Granted I don't need it very often...
I know in one program I overloaded Choose with a dozen type safe versions,
using Choose(Of T) will reduce it to a single version...
FWIW: I find Choose to be more useful then the Switch function, as its easy
to create a type safe Choose, however its hard (impossible?) to make a type
safe Switch function...
Hope this helps
Jay
"Urs Eichmann" <ur***@online.nospam> wrote in message
news:OA**************@tk2msftngp13.phx.gbl...
| Jay,
|
| > Urs,
| > | I know what generic classes are for, but a generic Sub? Does anybody
know
| > | why they are here?
| > They allow you to define a method (sub or function) that behaves
| > polymorphically across types. Just as generic classes allow you to
define
| > classes that behave polymorphically across types...
| >
| > Consider:
| >
| > ' VS.NET 2005 syntax
| > Public Function IIf(Of T)(ByVal expression As Boolean, _
| > ByVal truePart As T, ByVal falsePart As T) As T
| > If expression Then
| > Return truePart
| > Else
| > Return falsePart
| > End If
| > End Function
|
| thanks for the explanation. I guess I can also write
|
| Public Function ToArray(Of T)(vList as IList) As T()
| return directcast(vList.ToArray(T),T())
| End Function
|
| instead of the horrible
|
| dim a() as MyClass= DirectCast(vList.ToArray(gettype(MyClass)),MyClass ())
|
| I use today to convert an IList to an array. Now I guess I can just use
|
| dim a() as MyClass = ToArray(Of MyClass)(vList)
|
| I look forward to it already :-)
|
| >
| > NOTE: VB is able to figure out the types, so IIf(Of Integer) is not
| > specifically needed...
|
| is that new? 2003 doesn't do it
|
| >
| > I can see defining a similar routine for Choose, something like:
| >
| > ' VS.NET 2005 syntax
| > Public Function Choose(Of T)(ByVal index As Integer, _
| > ParamArray ByVal choice() As T) As T
| > Return choice(index-1)
| > End Function
|
| Boah, is somebody still using Choose? :-)
|
| Thanks for your help
| Urs