How to get rid of warning.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • enliven60411
    New Member
    • Dec 2009
    • 10

    #1

    How to get rid of warning.

    This the waring I get:
    Warning 4 Function 'UnmaskSSN' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.
    Code:
    Private Function UnmaskSSN(ByVal SSN As String) As String
    
            ' Yank dashes out of the SSN in the masked textbox
            If SSN = "   -  -    " Then
                SSN = "d"
            Else
                If Len(SSN) = 11 Then
                    Dim UnmaskedSSN As String = Microsoft.VisualBasic.Left(SSN, 3) & Microsoft.VisualBasic.Mid(SSN, 5, 2) & Microsoft.VisualBasic.Right(SSN, 4)
                    Return UnmaskedSSN
                Else
                    SSN = "d"
                    MessageBox.Show("The SSN entered is not complete." & vbCrLf & "Please try again.", _
                     "CT Guide Request Search", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                    Me.txtSSN.Focus()
                End If
    
            End If
    
        End Function
  • Oralloy
    Recognized Expert Contributor
    • Jun 2010
    • 988

    #2
    Well, the error code means exactly what it says.

    If you follow your code through each of the major code paths, you'll find that when the SSN is empty (your magic string), there is no return value; and when the length of SSN is not 11, there is no return value. Instead, in these paths you set the SSN variable to "d", instead.

    You might try something like this, instead:

    Code:
    Private Function UnmaskSSN(ByVal SSN As String) As String 
      Dim result as string
      result = "d"
    
      If Len(SSN) <> 11 Then 
        MessageBox.Show("The SSN entered is not complete." & _
                        vbCrLf & "Please try again.", _ 
                        "CT Guide Request Search", _
                        MessageBoxButtons.OK, _
                        MessageBoxIcon.Exclamation) 
        Me.txtSSN.Focus()
      Else If SSN = "   -  -    " Then 
        '' noOp - result already has "bad" value
      Else
        '' Yank dashes out of the SSN in the masked textbox 
        result = Microsoft.VisualBasic.Left(SSN, 3) & _
                 Microsoft.VisualBasic.Mid(SSN, 5, 2) & _
                 Microsoft.VisualBasic.Right(SSN, 4) 
      End If
    
      UnmaskSSN = result
    End Function

    Comment

    Working...