变量' Delimeter'在被赋值之前使用。在运行时可能会导致空引用异常

时间:2016-01-17 00:59:58

标签: vb.net

它给我带来了分隔符代码的错误

Public Function str_Filter(ByVal Text As TextBox, ByVal ascKey1 As Integer, ByVal ascKey2 As Integer, ByVal ascKey3 As Integer, ByVal N_Repeat As Integer)
    On Error Resume Next
    '-----function dump all strings except
    Dim Delimeter As String
    Dim X As Long
    Dim intStr As String
    Dim NumberToRepeatCharacter As Integer

    'MsgBox(Chr(Asc(Text.Text)))
    'MsgBox(Asc(Text.Text))
    For X = 1 To Len(Text.Text)         'asckey1                                asckey2                              asckey3
        If Asc(Mid((Text.Text), X, 1)) >= ascKey1 And Asc(Mid((Text.Text), X, 1)) <= ascKey2 Or Asc(Mid(Text.Text, X, 1)) = ascKey3 Then
        Else
            Delimeter = Chr(Asc(Mid(Text.Text, X, 1)))
            'MsgBox(Delimeter)
        End If
    Next
    intStr = ""
    For X = 1 To Len(Text.Text)
        If N_Repeat > 0 Then
            If Asc(Mid(Text.Text, X, 1)) = ascKey3 Then
                If NumberToRepeatCharacter >= N_Repeat Then
                    SendKeys.Send("{END}")
                    Exit For
                End If
                NumberToRepeatCharacter = NumberToRepeatCharacter + 1
            End If
        End If
        'If NumberToRepeatCharacter <> N_Repeat Then
        If Mid(Text.Text, X, 1) <> Delimeter Then
            intStr = intStr & Mid(Text.Text, X, 1)
            'NumberToRepeatCharacter = NumberToRepeatCharacter + 1
        Else
            'can be uncomment if you want
            'SendKeys.Send("{END}")
        End If
        ' Else
        ' SendKeys.Send("{END}")
        ' End If

        'NumberToRepeatCharacter = X
    Next
    'MsgBox(NumberToRepeatCharacter)
    str_Filter = intStr
End Function*emphasized text*

如何修复Warning Variable 'Delimeter' is used before it has been assigned a value. A null reference exception could result at runtime

1 个答案:

答案 0 :(得分:0)

Private Delimeter As String = Nothing
Public Function str_Filter(ByVal Text As TextBox, ByVal ascKey1 As Integer, ByVal ascKey2 As Integer, ByVal ascKey3 As Integer, ByVal N_Repeat As Integer)
    On Error Resume Next
    '-----function dump all strings except

    Dim X As Long
    Dim intStr As String
    Dim NumberToRepeatCharacter As Integer

    'MsgBox(Chr(Asc(Text.Text)))
    'MsgBox(Asc(Text.Text))
    For X = 1 To Len(Text.Text)         'asckey1                                asckey2                              asckey3
        If Asc(Mid((Text.Text), X, 1)) >= ascKey1 And Asc(Mid((Text.Text), X, 1)) <= ascKey2 Or Asc(Mid(Text.Text, X, 1)) = ascKey3 Then
        Else
            Delimeter = Chr(Asc(Mid(Text.Text, X, 1)))
            'MsgBox(Delimeter)
        End If
    Next
    intStr = ""
    For X = 1 To Len(Text.Text)
        If N_Repeat > 0 Then
            If Asc(Mid(Text.Text, X, 1)) = ascKey3 Then
                If NumberToRepeatCharacter >= N_Repeat Then
                    SendKeys.Send("{END}")
                    Exit For
                End If
                NumberToRepeatCharacter = NumberToRepeatCharacter + 1
            End If
        End If
        'If NumberToRepeatCharacter <> N_Repeat Then
        If Mid(Text.Text, X, 1) <> Delimeter Then
            intStr = intStr & Mid(Text.Text, X, 1)
            'NumberToRepeatCharacter = NumberToRepeatCharacter + 1
        Else
            'can be uncomment if you want
            'SendKeys.Send("{END}")
        End If
        ' Else
        ' SendKeys.Send("{END}")
        ' End If

        'NumberToRepeatCharacter = X
    Next
    'MsgBox(NumberToRepeatCharacter)
    str_Filter = intStr
End Function*emphasized text*