在for循环VB期间将项添加到List(Of)

时间:2016-07-06 00:48:07

标签: vb.net parsing

我正在尝试将文本框中的每个数字数字添加到一个列表(Integer)中,但我需要使用integer.Parse将其转换为正确的格式,但这样做会收到" Object引用未设置为对象的实例"错误。 我的代码如下:

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 个答案:

答案 0 :(得分:1)

您必须初始化列表的实例。这是使用New关键字完成的:

Dim key As New List(Of Integer)

详细了解New keyword

相关问题