【发布时间】:2015-07-30 23:06:58
【问题描述】:
我在 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
但是 VS2005 对“If OutFile IsNot..”这一行提出了警告
变量“OutFile”在被赋值之前被使用。运行时可能会导致空引用异常。
是否有某种方法可以通过巧妙地更改代码来消除此警告,或者是否有更好的方法来做我想做的事情?
谢谢
罗伯
【问题讨论】: