Compiler warning: null reference exception

I have the following code in Visual Studio 2005.

    Dim OutFile As System.IO.StreamWriter
    Try
        OutFile = New System.IO.StreamWriter(Filename)
       // Do stuff with OutFile
    Catch Ex As Exception
       // Handle Exception
    Finally
       If OutFile IsNot Nothing Then OutFile.Close()
    End Try

But VS2005 brings up the warning for the line "If OutFile IsNot.." that

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

Is there some way of removing this warning by subtly altering the code or is there just a better way of doing what I'm trying to do?

Thanks

Rob

Answers


Dim OutFile As System.IO.StreamWriter
OutFile = Nothing
Try
    OutFile = New System.IO.StreamWriter(Filename)
   // Do stuff with OutFile
Catch Ex As Exception
   // Handle Exception
Finally
   If OutFile IsNot Nothing Then OutFile.Close()
End Try

Similar to http://stackoverflow.com/questions/256073/c-error-use-of-unassigned-local-variable#256078


Need Your Help

Skip Glympse wizard view iOS

c++ ios send wizard glympse

I want to integrate Glympse in my application, but without the wizard view.

How to remove svn url from commit messages?

git github git-svn

I'm trying to push a git-svn repo to github but I don't want the url to appear in my commit messages.