2015-11-21 66 views
0

我有一個特定的陣列整數如何清空或重置或清除此類型的數組?

Dim ArrayWinRandomNumber(0 To 5) As Integer 

和其他功能上把它

Private Sub repetido() 
    For x = 0 To 5 
     Randomize() 
     Dim RandomNumber As Integer 
     RandomNumber = CInt(Int((49 * Rnd()) + 1)) 
     For y = 0 To 5 
      If RandomNumber = ArrayWinRandomNumber(y) Then 
       repetido() 
      End If 
     Next 
     ArrayWinRandomNumber(x) = RandomNumber 
    Next x 
    ordenar() 
End Sub 

的問題是,他第一次叫重複,它工作正常,但如果你再打第二次。我回到IndexOutOfRangeException

+1

'使用ReDim ArrayWinRandomNumber(0對於5)'或'Array.Clear(ArrayWinRandomNumber,0,6)'有更好的方法來獲得一組不重複的值,雖然 – Plutonix

+0

它的工作對我來說 – Cazs

+0

你看到我的答案了嗎?你可以用1行代碼實現隨機數字陣列 –

回答

1

你或許應該做這樣的事情(你用vb.net像VB6)

Imports System.Linq 
. . . . . . . . . . . 
Dim r as new Random() 
Dim items() as Integer = Enumerable.Range(1, 5).Select(Function(i) r.Next()).ToArray() 

For Each i as Integer in items 
    console.WriteLine("Item val: " & i) 
Next 

它將給隨機數的數組,你任何時候