vb.net – 编译器警告:null引用异常

前端之家收集整理的这篇文章主要介绍了vb.net – 编译器警告:null引用异常前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在Visual Studio 2005中有以下代码.
  1. Dim OutFile As System.IO.StreamWriter
  2. Try
  3. OutFile = New System.IO.StreamWriter(Filename)
  4. // Do stuff with OutFile
  5. Catch Ex As Exception
  6. // Handle Exception
  7. Finally
  8. If OutFile IsNot Nothing Then OutFile.Close()
  9. End Try

但VS2005提出警告“If OutFile IsNot ..”这一行

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

有没有办法通过巧妙地改变代码来消除这个警告,或者只是有更好的方法来做我正在尝试做的事情?

谢谢

  1. Dim OutFile As System.IO.StreamWriter
  2. OutFile = Nothing
  3. Try
  4. OutFile = New System.IO.StreamWriter(Filename)
  5. // Do stuff with OutFile
  6. Catch Ex As Exception
  7. // Handle Exception
  8. Finally
  9. If OutFile IsNot Nothing Then OutFile.Close()
  10. End Try

C# error: Use of unassigned local variable相似

猜你在找的VB相关文章