I am trying to ReDim a member array based on reading a file. I cannot figure out how to do it. This is what I tried, but it does not work.
Public Class BS
Public A() As String
Public B() As Double
Public C() As Double
End Class
Public Class SB
Public MyBS() As BS
'ReadFieldString is a function that returns a string of the field name of Class BS,
'i.e., A, B or C. For test purpose, retun a constant
Public Function ReadFieldString() As String
Return "B"
End Function
'GetArrayDim is a function that returns an integer, which is the size of the array
'of that field name. For test purpose, retun a constant
Public Function GetArrayDim() As Integer
Return 1
End Function
Public Sub DimArrays()
ReDim MyBS(3)
Dim i As Integer
For i = 0 To MyBS.Length - 1
'Try to ReDim the member of MyBS
ReDim MyBS(i).GetType.GetField(ReadFieldString)(GetArrayDim)
Next()
End Sub
End Class
The ReDim statement has the error "Expression is a value and therefore cannot be the target of an assignment." Thanks in advance.
I'm not sure ReDim
works like that. Changing the code to this will achieve what I believe you are after:
Public Sub DimArrays() ReDim MyBS(3) Dim i As Integer For i = 0 To MyBS.Length - 1 MyBS(i) = New BS() Dim f = GetType(BS).GetField(ReadFieldString()) f.SetValue(MyBS(i), Array.CreateInstance(f.FieldType.GetElementType(), GetArrayDim())) Next End Sub
However, I think a better approach would be to specify the array size in the BS constructor.
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.