Zexor
Well-known member
- Joined
- Nov 28, 2008
- Messages
- 520
- Programming Experience
- 3-5
Can these 2 for loops be combine into one function where you input the 2DArray and the type to convert into, and it output an array of the customTypes?
I am guessing it be something like this but how do you pass different custom type on a variable? and how do u go thru each item of the custom type and fill in the correct value?
VB.NET:
Private Class sCustomType
Public Property A As String
Public Property B As String
End Class
Private Class tCustomType
Public Property C As String
Public Property D As String
Public Property E As String
End Class
Dim sArray(,) as string
dim s() as sCustomType
Dim tArray(,) as string
dim t() as tCustomType
.
.
For i As Integer = 0 To sArray.GetUpperBound(1)
ReDim Preserve s(i)
s(i) = New sCustomType
With s(i)
.A = sArray(0, i)
.B = sArray(1, i)
End With
Next
For i As Integer = 0 To tArray.GetUpperBound(1)
ReDim Preserve t(i)
t(i) = New tCustomType
With t(i)
.C = tArray(0, i)
.D = tArray(1, i)
.E = tArray(2, i)
End With
Next
I am guessing it be something like this but how do you pass different custom type on a variable? and how do u go thru each item of the custom type and fill in the correct value?
VB.NET:
Dim sArray(,) as string
dim s() as sCustomType
Dim tArray(,) as string
dim t() as tCustomType
.
.
s=directcast(twoDArrayToObjectArray(sArray, sCustomType),sCustomType)
t=directcast(twoDArrayToObjectArray(tArray, tCustomType),tCustomType)
Private Function twoDArrayToObjectArray(twoDArray(,) As String, customType As Type) As Object()
Dim output() As customType 'not sure how to pass different custom types
For i As Integer = 0 To twoDArray.GetUpperBound(1)
ReDim Preserve output(i)
output(i) = New customType
dim J as integer =0
For Each item In output(i) ' not sure how to go thru each item
item() = twoDArray(j, i)
j+=1
Next
Next
return output
End Function
Last edited: