2016-07-06 283 views
0

我想將文本框中的每個數字數字添加到列表(整數),但我需要使用integer.Parse將其轉換爲正確的格式,但這樣做會收到「未設置爲對象實例的對象引用」錯誤。 我的代碼如下:在for循環VB中添加項目(Of)VB

Dim key As List(Of Integer) 
Dim digit As Integer 

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 
    For Each c As Char In TextBox1.Text 
     digit = Integer.Parse(c) 
     key.Add(digit) 
    Next 
End Sub 
+1

'Dim'只聲明瞭你想要一個名爲'key'變量是'名單(的Int32)已'。它不會創建集合對象。 – Plutonix

回答

1

您必須初始化列表的一個實例。這是使用New關鍵字進行:

Dim key As New List(Of Integer) 

瞭解更多關於New keyword

+0

非常感謝! – Ghoohio