0

i write this code in my program to encrypt my files but it showing me warning

Warning 1

Variable 'csCryptoStream' is used before it has been assigned a value. A null reference exception could result at runtime

please guide me. when i run the software it work fine but i want to remove this warning so please help me

3
Contributors
5
Replies
14
Views
2 Years
Discussion Span
Last Post by OMER AHMED
0

here is the code

Dim csCryptoStream As CryptoStream
Dim cspRijndael As New System.Security.Cryptography.RijndaelManaged
pbStatus.Value =

   Dim csCryptoStream As CryptoStream
            'Declare your CryptoServiceProvider.
            Dim cspRijndael As New System.Security.Cryptography.RijndaelManaged
            'Setup Progress Bar
            pbStatus.Value = 0
            pbStatus.Maximum = 100

            'Determine if ecryption or decryption and setup CryptoStream.
            Select Case Direction
                Case CryptoAction.ActionEncrypt
                    csCryptoStream = New CryptoStream(fsOutput, _
                    cspRijndael.CreateEncryptor(bytKey, bytIV), _
                    CryptoStreamMode.Write)

                Case CryptoAction.ActionDecrypt
                    csCryptoStream = New CryptoStream(fsOutput, _
                    cspRijndael.CreateDecryptor(bytKey, bytIV), _
                    CryptoStreamMode.Write)
            End Select

            'Use While to loop until all of the file is processed.
            While lngBytesProcessed < lngFileLength
                'Read file with the input filestream.
                intBytesInCurrentBlock = fsInput.Read(bytBuffer, 0, 4096)
                'Write output file with the cryptostream.
                csCryptoStream.Write(bytBuffer, 0, intBytesInCurrentBlock)
                'Update lngBytesProcessed
                lngBytesProcessed = lngBytesProcessed + CLng(intBytesInCurrentBlock)
                'Update Progress Bar
                pbStatus.Value = CInt((lngBytesProcessed / lngFileLength) * 100)
            End While

            'Close FileStreams and CryptoStream.
            csCryptoStream.Close()
            fsInput.Close()
            fsOutput.Close()

0
pbStatus.Maximum = 100

        Select Case Direction
            Case CryptoAction.ActionEncrypt
                csCryptoStream = New CryptoStream(fsOutput, _
                cspRijndael.CreateEncryptor(bytKey, bytIV), _
                CryptoStreamMode.Write)

            Case CryptoAction.ActionDecrypt
                csCryptoStream = New CryptoStream(fsOutput, _
                cspRijndael.CreateDecryptor(bytKey, bytIV), _
                CryptoStreamMode.Write)
        End Select


        While lngBytesProcessed < lngFileLength

            intBytesInCurrentBlock = fsInput.Read(bytBuffer, 0, 4096)

            csCryptoStream.Write(bytBuffer, 0, intBytesInCurrentBlock)

            lngBytesProcessed = lngBytesProcessed + CLng(intBytesInCurrentBlock)

            pbStatus.Value = CInt((lngBytesProcessed / lngFileLength) * 100)
        End While


        csCryptoStream.Close()
        fsInput.Close()
        fsOutput.Close()

Edited by OMER AHMED

0

this is the warning

Warning 1 Variable 'csCryptoStream' is used before it has been assigned a value. A null reference exception could result at runtime.

0

Have you tried replacing:

Dim csCryptoStream As CryptoStream

With

Dim csCryptoStream As New CryptoStream

What this warning is telling you is that the variable could throw a null reference exception if there is a dependant variable that requires instantiation. By not declaring the variable with the New modifier, you are possibly not creating any dependant arrays, variables, objects, ect....

Edited by Begginnerdev

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.