Resolved Doesn't Return a Value on all Code Paths

Bernie

Well-known member
Joined
Aug 13, 2007
Messages
98
Programming Experience
3-5
This problem has annoyed me for a while now and I'd like to find a solution. I have a function that creates an ODBCDataReader. I put the code in a try block to capture any problems with the query. I get a warning that it "doesn't return a value on all code paths." That is correct because if there is an exception then the datareader is not filled.

What should I return in this case?

Can I create an empty datareader or a null device?

Thanks,
Bernie
 
Last edited:
Can I create an empty datareader or a null device?
Data readers don't have public constructors, they are returned from a ExecuteReader call or a DataSet/DataTable.CreateDataReader call. The latter returns a DataTableReader, the former for example a OdbcDataReader, they both inherits DbDataReader. So it is possible to create a new empty DataTable and CreateDataReader from this, thus for both cases return a DbDataReader. If you look at the OdbcDataReader member list you see all members inherit or overrides DbDataReader members, so you get no problems with type checking or casting using this base type later in code.

You can also choose to return Nothing, which is what you are doing now implicitly because Nothing is the default return value for reference types. This design forces you to check for Nothing when using the function before you attempt to use the reader, this is also what second part of the warning says:
A null reference exception could occur at run time when the result is used.
To remove the warning explicitly "Return Nothing" for other cases.
 
John,

Thanks for the reply! In putting some more thought to the issue, I think I'll switch to handing datatables back and forth to functions or encapsulating into classes. The only reason I was using datareaders is because the book I read back in 2003 recommended it, hahahaha. Its funny how we collect our bad habits.

I think it will be easier to deal with potential empty tables in the main code than the possibilities of null readers.

Thanks again.
Bernie
 
Back
Top