<p>I have the following code in Visual Studio 2005.</p>

<pre><code>    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
</code></pre>

<p>But VS2005 brings up the warning for the line "If OutFile IsNot.." that </p>

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

<p>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?</p>

<p>Thanks</p>

<p>Rob</p>

```
```
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
C# error: Use of unassigned local variable