Testing for OleDbDataReader is nothing before closing it

  • Thread starter Thread starter Jerry Spence1
  • Start date Start date
J

Jerry Spence1

I have been doing the following is VB.Net 2003:

Dim dr1 As OleDbDataReader


.....Code here



If Not dr1 Is Nothing Then

If Not dr1.IsClosed Then
dr1.Close()
end if

End Try


However in VB2005 I get the (correct) error message

dr1 is used before it has been assigned a value. A Null reference exception
could result at run time.

What syntax should I be using now?

Thanks

-Jerry
 
You have 2 options


1. set this naging warning off

2. always assign default values

in the case of a object without a constructor assign Nothing
Dim dr As OleDb.OleDbDataReader = Nothing
otherwise just construct the object ( foo As New .......)

regards

Michel Posseth [MCP]
 
Back
Top