none
How to Fix CA2000 and CA1305 Warning in this code. RRS feed

  • Question

  • Hi,

    Please Help me Resolve this warning ( CA2000 and CA1305) according to FxCop

    Public Shared Function GetNISTTime(ByVal host As String) As DateTime
            If K9Core.CheckInternetConn = True Then
                Dim timeStr As String = ""
                Dim reader As New StreamReader(New TcpClient(host, 13).GetStream) 'CA2000 Warning
    
                Try
                    K9Core.LastSysTime = DateTime.UtcNow()
                    timeStr = reader.ReadToEnd()
                    reader.Close()
                Catch ex As SocketException
                    InternetTime = ex.Message
                Catch ex As Exception
                    InternetTime = ex.Message
                    Throw
                End Try
    
                Dim jd As Integer = Integer.Parse(timeStr.Substring(1, 5))      'CA1305 Warning
                Dim yr As Integer = Integer.Parse(timeStr.Substring(7, 2))      'CA1305 Warning
                Dim mo As Integer = Integer.Parse(timeStr.Substring(10, 2))     'CA1305 Warning
                Dim dy As Integer = Integer.Parse(timeStr.Substring(13, 2))     'CA1305 Warning
                Dim hr As Integer = Integer.Parse(timeStr.Substring(16, 2))     'CA1305 Warning
                Dim mm As Integer = Integer.Parse(timeStr.Substring(19, 2))     'CA1305 Warning
                Dim sc As Integer = Integer.Parse(timeStr.Substring(22, 2))     'CA1305 Warning
                Dim Temp As Integer = CInt(AscW(timeStr(7)))
    
                InternetTime = Format(Now, "yyyy-MM-dd h:mm tt")
    
                Return New DateTime(yr + 2000, mo, dy, hr, mm, sc)
    
            End If
            Return Now
        End Function

    Thank You.. 

    Rohan~


    RohanD

    Sunday, August 4, 2019 11:20 PM

Answers


  • Please Help me Resolve this warning ( CA2000 and CA1305) according to FxCop

    
                Dim jd As Integer = Integer.Parse(timeStr.Substring(1, 5))      'CA1305 Warning
                Dim yr As Integer = Integer.Parse(timeStr.Substring(7, 2))      'CA1305 Warning
                Dim mo As Integer = Integer.Parse(timeStr.Substring(10, 2))     'CA1305 Warning
                Dim dy As Integer = Integer.Parse(timeStr.Substring(13, 2))     'CA1305 Warning
                Dim hr As Integer = Integer.Parse(timeStr.Substring(16, 2))     'CA1305 Warning
                Dim mm As Integer = Integer.Parse(timeStr.Substring(19, 2))     'CA1305 Warning
                Dim sc As Integer = Integer.Parse(timeStr.Substring(22, 2))     'CA1305 Warning
    

    You should be able to eliminate the CA1305 warnings by specifying the Culture.
    Such as in these examples:

    'Dim jd As Integer = Integer.Parse(timeStr.Substring(1, 5))  'CA1305 Warning
    Dim jd As Integer = Integer.Parse(timeStr.Substring(1, 5), CultureInfo.CurrentCulture())
    
    'Dim yr As Integer = Integer.Parse(timeStr.Substring(7, 2))      'CA1305 Warning
    Dim yr As Integer = Integer.Parse(timeStr.Substring(7, 2), CultureInfo.InvariantCulture())
    

    - Wayne

    • Marked as answer by RAT_SL Monday, August 5, 2019 7:27 AM
    Monday, August 5, 2019 12:23 AM

  • Please Help me Resolve this warning ( CA2000 and CA1305) according to FxCop

            If K9Core.CheckInternetConn = True Then
                Dim timeStr As String = ""
                Dim reader As New StreamReader(New TcpClient(host, 13).GetStream) 'CA2000 Warning
    
                Try
                    K9Core.LastSysTime = DateTime.UtcNow()
                    timeStr = reader.ReadToEnd()
                    reader.Close()
                Catch ex As SocketException
                    InternetTime = ex.Message
                Catch ex As Exception
                    InternetTime = ex.Message
                    Throw
                End Try
    
    

    Eliminating the CA2000 may require a little more work. I suspect that the Warning
    you're getting refers to the TcpClient object. It wants you to Dispose of it
    before all references to it go out of scope.

    Now since the way you have written it makes the TcpClient object anonymous you
    have no way to explicitly invoke its Dispose. So the first step would be to
    break your compound statement into its component parts so that the TcpClient
    object has a name:

    'Dim reader As New StreamReader(New TcpClient(host, 13).GetStream) 'CA2000 Warning
    
    Dim tc As TcpClient = New TcpClient(host, 13)
    Dim reader As New StreamReader(tc.GetStream)
    

    Now you can explicitly call tc.Dispose(), probably before reader.Close().

    However, even then you probably will still get a CA2000 because reader.ReadToEnd()
    may throw an exception. So you would/will have to Dispose of tc in the catch
    blocks as well. See this article:

    How to Avoid the CA2000 Error from Code Analysis
    http://www.fascinatedwithsoftware.com/blog/post/2011/06/17/How-to-Avoid-the-CA2000-Error-from-Code-Analysis.aspx

    - Wayne

    • Marked as answer by RAT_SL Monday, August 5, 2019 7:26 AM
    Monday, August 5, 2019 1:12 AM
  • Note that an alternative to explicitly Disposing of the TcpClient object in both
    Try and Catch blocks you should be able to place it in a Using statement. For
    example:

    Using tc As TcpClient = New TcpClient(host, 13)
        Dim reader As New StreamReader(tc.GetStream)
        timeStr = reader.ReadToEnd()
        reader.Close()
    End Using
    

    That should eliminate the CA2000 warning.

    - Wayne


    • Marked as answer by RAT_SL Monday, August 5, 2019 7:26 AM
    Monday, August 5, 2019 4:10 AM

All replies


  • Please Help me Resolve this warning ( CA2000 and CA1305) according to FxCop

    
                Dim jd As Integer = Integer.Parse(timeStr.Substring(1, 5))      'CA1305 Warning
                Dim yr As Integer = Integer.Parse(timeStr.Substring(7, 2))      'CA1305 Warning
                Dim mo As Integer = Integer.Parse(timeStr.Substring(10, 2))     'CA1305 Warning
                Dim dy As Integer = Integer.Parse(timeStr.Substring(13, 2))     'CA1305 Warning
                Dim hr As Integer = Integer.Parse(timeStr.Substring(16, 2))     'CA1305 Warning
                Dim mm As Integer = Integer.Parse(timeStr.Substring(19, 2))     'CA1305 Warning
                Dim sc As Integer = Integer.Parse(timeStr.Substring(22, 2))     'CA1305 Warning
    

    You should be able to eliminate the CA1305 warnings by specifying the Culture.
    Such as in these examples:

    'Dim jd As Integer = Integer.Parse(timeStr.Substring(1, 5))  'CA1305 Warning
    Dim jd As Integer = Integer.Parse(timeStr.Substring(1, 5), CultureInfo.CurrentCulture())
    
    'Dim yr As Integer = Integer.Parse(timeStr.Substring(7, 2))      'CA1305 Warning
    Dim yr As Integer = Integer.Parse(timeStr.Substring(7, 2), CultureInfo.InvariantCulture())
    

    - Wayne

    • Marked as answer by RAT_SL Monday, August 5, 2019 7:27 AM
    Monday, August 5, 2019 12:23 AM

  • Please Help me Resolve this warning ( CA2000 and CA1305) according to FxCop

            If K9Core.CheckInternetConn = True Then
                Dim timeStr As String = ""
                Dim reader As New StreamReader(New TcpClient(host, 13).GetStream) 'CA2000 Warning
    
                Try
                    K9Core.LastSysTime = DateTime.UtcNow()
                    timeStr = reader.ReadToEnd()
                    reader.Close()
                Catch ex As SocketException
                    InternetTime = ex.Message
                Catch ex As Exception
                    InternetTime = ex.Message
                    Throw
                End Try
    
    

    Eliminating the CA2000 may require a little more work. I suspect that the Warning
    you're getting refers to the TcpClient object. It wants you to Dispose of it
    before all references to it go out of scope.

    Now since the way you have written it makes the TcpClient object anonymous you
    have no way to explicitly invoke its Dispose. So the first step would be to
    break your compound statement into its component parts so that the TcpClient
    object has a name:

    'Dim reader As New StreamReader(New TcpClient(host, 13).GetStream) 'CA2000 Warning
    
    Dim tc As TcpClient = New TcpClient(host, 13)
    Dim reader As New StreamReader(tc.GetStream)
    

    Now you can explicitly call tc.Dispose(), probably before reader.Close().

    However, even then you probably will still get a CA2000 because reader.ReadToEnd()
    may throw an exception. So you would/will have to Dispose of tc in the catch
    blocks as well. See this article:

    How to Avoid the CA2000 Error from Code Analysis
    http://www.fascinatedwithsoftware.com/blog/post/2011/06/17/How-to-Avoid-the-CA2000-Error-from-Code-Analysis.aspx

    - Wayne

    • Marked as answer by RAT_SL Monday, August 5, 2019 7:26 AM
    Monday, August 5, 2019 1:12 AM
  • Note that an alternative to explicitly Disposing of the TcpClient object in both
    Try and Catch blocks you should be able to place it in a Using statement. For
    example:

    Using tc As TcpClient = New TcpClient(host, 13)
        Dim reader As New StreamReader(tc.GetStream)
        timeStr = reader.ReadToEnd()
        reader.Close()
    End Using
    

    That should eliminate the CA2000 warning.

    - Wayne


    • Marked as answer by RAT_SL Monday, August 5, 2019 7:26 AM
    Monday, August 5, 2019 4:10 AM
  • Thank you so much... it's working without warning now... 

    RohanD

    Monday, August 5, 2019 7:28 AM