當我使用變量定義整數數組的大小時,出現錯誤:「IndexOutOfRangeException was unhandled」。但是,如果我只是把與我使用的變量相同的值,它就可以工作。當尺寸由變量定義時,爲什麼索引超出範圍?
我會更好下面的評論解釋:
Public Class Form1
Dim test As Test
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
test = New Test(5) 'the length property is test is 5
test.AddToList()
End Sub
End Class
Public Class Test
Dim _length As Integer
Public Property length() As Integer
Get
Return _length
End Get
Set(ByVal value As Integer)
_length = value
End Set
End Property
Dim _magnitude(length, 2) As Integer 'Size is length, which should be equal to 5. If I remove length and just put 5, it works fine.
Public Property magnitude As Integer(,)
Get
Return _magnitude
End Get
Set(ByVal value As Integer(,))
_magnitude = value
End Set
End Property
Public Sub New(ByVal lengthp As Integer)
length = lengthp 'Sets 5 to the length property.
End Sub
Public Sub AddToList()
magnitude(4, 0) = 4 'Operates on the magnitude property. This is where the error is located.
Debug.Print(magnitude(4, 0))
End Sub
End Class
希望你們明白我在問什麼。