如何解决索引超出了vb中数组的界限,见下面的编码

问题描述:

读取阅读按钮的信息

Read in information for Read button

Private Sub btnRead_Click(sender As Object, e As EventArgs) Handles btnRead.Click
    Dim F As Integer
    Dim P As Integer

    For F = 1 To NumFarmers
        MarsFarmers(F).Name = InputBox("Please enter farmer name" & CStr(F))
        MarsFarmers(F).Workers = CInt(InputBox("Please enter number of workers" & CStr(F)))
        For P = 1 To NumPeriods
            MarsFarmers(F).Profit(P) = CInt(InputBox("Please enter profit of farmer" & CStr(F)))
        Next P
    Next F
End Sub

数组从 0开始(零)到 count -1 除非选项库 [ ^ ]设置为 1



请参阅:

数组在Visual Basic中 [ ^ ]
Arrays start from 0 (zero) to count -1 unless Option Base[^] is set to 1.

See:
Arrays in Visual Basic[^]


NumFarmers 的值是多少? MarsFarmers 数组应具有此长度。



同样, NumPeriods的值是多少?每个MarsFarmer的利润数组都应该有这个长度。



如何实例化/初始化这些数组?



否则,解决这个问题的最简单方法是在方法开头加上一个beakpoint,按F5并按F11开始逐行调试;仔细观察每个步骤中的每个相关变量,并确认它与您认为的相应。
What is the value of NumFarmers? MarsFarmers array should have this length.

Similarly, what is the value of NumPeriods? The Profit array for each "MarsFarmer" should have this length.

How do you instantiate/initialize these arrays?

Otherwise, the simplest way to solve that is to put a beakpoint at the beginning of the method, press F5 and start debugging line by line by pressing F11; carefully watch for every relevant variable at each step, and confirm it is equal to what you think it should.