我在尝试检查项目是否在字典中时出错

时间:2016-05-23 17:07:30

标签: vb.net

贝娄是我的vb代码,我试图遍历输入的所有单词并将所有独特单词放入带有位置的字典中。我收到此错误,但我不知道如何修复它。拜托,我可以提出任何建议吗?谢谢。

错误:对象引用未设置为对象的实例。

Public Class Form1
    Dim sentence() As String
    Dim uniqueWords As Dictionary(Of String, Integer)

    Private Sub creatSaveBtn_Click(sender As Object, e As EventArgs) Handles creatSaveBtn.Click
        sentence = sentenceInputTxt.Text.ToLower.Split(" ")

        For Each word In sentence
            If Not uniqueWords.ContainsKey(word) Then
                uniqueWords.Add(word, uniqueWords.Count + 1)
            End If
        Next
    End Sub
End Class

1 个答案:

答案 0 :(得分:2)

Dim uniqueWords As New Dictionary(Of String, Integer)

你也可以:

Dim uniqueWords As List(Of String) = sentenceInputTxt.Text.ToLower.Split(" "c).Distinct().ToList()