我正在尝试初始化一个数组并分配值,但我遇到了问题
Public Structure test1
<VBFixedArray(6)> Dim Id() As String
Public Sub initialize()
ReDim Id(6)
End Sub
End Structure
Dim myvalues() as test1
...
ReDim myvalues(10)
我正在尝试通过以下方式完成此操作
For i as short = 0 to 10
myvalues(i).Id(1) = "V"
myvalues(i).Id(2) = "H"
...
myvalues(i).Id(6) = "J"
next i
这不起作用,我不确定正确的实施?
编辑:
Imports Microsoft.VisualBasic
Module Module1
Structure test1
<VBFixedArray(6)> Dim Id() As String
Public Sub initialize()
ReDim Id(6)
For im As Integer = 0 To 6
Id(im) = ""
Next im
End Sub
End Structure
Sub Main()
Dim i As Integer
Dim myvalues() As test1
ReDim myvalues(10)
For Each oTest As test1 In myvalues
' Initialize the structure
ReDim myvalues(10)
oTest.initialize()
' Now loop through the array based on the bounds it was configured for
'initialize to empty string(NOT ABLE TO ASSIGN TO> myvalues(i).Id(j))
'I Get ERROR>>>Object reference not set to an instance of an object<<<<<
'For i = 0 To 10
For j As Integer = oTest.Id.GetLowerBound(0) To oTest.Id.GetUpperBound(0)
' Set the values in a case statement so that alterations to the size of the index won't break your code.
Select Case j
Case 0
oTest.Id(j) = "A"
myvalues(i).Id(j) = "A"
Case 1
oTest.Id(j) = "B"
'myvalues(i).Id(j) = "B"
Case 2
oTest.Id(j) = "C"
'myvalues(i).Id(j) = "C"
Case 3
oTest.Id(j) = "D"
'myvalues(i).Id(j) = "D"
Case 4
oTest.Id(j) = "E"
'myvalues(i).Id(j) = "E"
Case 5
oTest.Id(j) = "F"
'myvalues(i).Id(j) = "F"
Case 6
oTest.Id(j) = "G"
'myvalues(i).Id(j) = "G"
End Select
Next j
'Next i
Next 'oTest
'assign<<<<<<<<<<<<<<<<<<< NOT WORKING>>>>
'<<Id() is not set to <empty string> it is "Nothing" I GET ERROR (91) next line and after
'What am I doing wrong?
For i = 0 To 10
Console.WriteLine(myvalues(i).Id(0), myvalues(i).Id(1), myvalues(i).Id(2), myvalues(i).Id(3), myvalues(i).Id(4), myvalues(i).Id(5), myvalues(i).Id(6))
Next i
End Sub
End Module