0
我的目標是創建一個特定大小的列表並從該列表中隨機選擇3個點。一旦選擇了一個點,它將從列表中刪除,因此它不會再被選中。 我的問題是調試器說我有一個索引超出列表的範圍。我對VB.NET比較陌生,但對VBA非常有經驗。預先感謝您的幫助。VB.NET;隨機生成列表的索引錯誤
Private Sub Analyze_Click(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles Analyze_Image.Click
Dim i As Long, j As Long, k As Long, Image_Width As Long, Image_Height As Long, Pixel_Count As Long, Pixel As Long
Dim My_Image As Image
Dim RandomClass As New Random()
Dim RandomIndex_1 As Long, RandomIndex_2 As Long, RandomIndex_3 As Long
Dim P1 As Point, P2 As Point, P3 As Point
My_Image = My_PictureBox.Image
Image_Width = My_Image.Width
Image_Height = My_Image.Height
Pixel_Count = Image_Width * Image_Height
Dim Numbers = Enumerable.Range(0, Pixel_Count + 1).ToList()
For Pixel = 1 To Pixel_Count
RandomIndex_1 = RandomClass.Next(0, Numbers.Count)
Numbers.RemoveAt(RandomIndex_1)
RandomIndex_2 = RandomClass.Next(0, Numbers.Count)
Numbers.RemoveAt(RandomIndex_2)
RandomIndex_3 = RandomClass.Next(0, Numbers.Count)
Numbers.RemoveAt(RandomIndex_3)
i = Numbers(RandomIndex_1)
j = Numbers(RandomIndex_2)
k = Numbers(RandomIndex_3)
If i Mod Image_Width = 0 Then
P1.X = Image_Width
Else
P1.X = i Mod Image_Width
End If
If j Mod Image_Width = 0 Then
P2.X = Image_Width
Else
P2.X = j Mod Image_Width
End If
If k Mod Image_Width = 0 Then
P3.X = Image_Width
Else
P3.X = k Mod Image_Width
End If
If ((i - i Mod Image_Width)/Image_Width) + 1 = Image_Height + 1 Then
P1.Y = Image_Height
Else
P1.Y = ((i - i Mod Image_Width)/Image_Width) + 1
End If
If ((j - j Mod Image_Width)/Image_Width) + 1 = Image_Height + 1 Then
P2.Y = Image_Height
Else
P2.Y = ((j - j Mod Image_Width)/Image_Width) + 1
End If
If ((k - k Mod Image_Width)/Image_Width) + 1 = Image_Height + 1 Then
P3.Y = Image_Height
Else
P3.Y = ((k - k Mod Image_Width)/Image_Width) + 1
End If
Next
End Sub
它可能會有助於表明哪一行拋出異常,但它看起來像你來回移除項目列表('Numbers.RemoveAt(RandomIndex_1)'),然後嘗試讀取它們('i = Numbers(RandomIndex_1)')。 – Mark
它也看起來像你需要'數字'列表中的'3 * Pixel_Count'項目? – Mark
令人驚訝的簡單修復,我不知道我的大腦今天在哪裏。進行了這兩項變更,現在正在開展工作。謝謝! – senuba91