I'm trying to create a function t开发者_开发问答o parse out all values in a multidimensional Array with all but one dimension given. The details are not relevant, but for this function I need to return an one-dimensional Array containing values of the same type the original multidimensional Array has.
To pass any Array with any dimension to my function, I declared the type of this parameter as Array. However, how would I create a new Array of that specific type (e.g. Integer)?
Currently I have the following code:
Function GetRow(ByVal arr As Array) As Array
Dim result As (...) 'This should be Integer() if arr contains Integers, etc.
Return result
End Function
How do I declare the type of result to make it having the same type of values as arr? New Array is not possible as it is declared MustInherit.
Use generics here so the function can handle any type:
Function GetRow(Of T)(ByVal arr() As T) As T()
Dim result() As T
ReDim result(arr.Length - 1)
Array.Copy(arr, result, arr.Length)
Return result
End Function
Sample usage:
Dim iarr() As Integer = {1, 2, 3, 4}
Dim copy = GetRow(iarr)
加载中,请稍侯......
精彩评论