Below code is giving me error like ''function doesnt return in all code paths A null reference exception could occur"
As i m new to this tech i am not getting that
PublicSharedFunction getLookupCampusList(ByVal eidAsString, _
ByVal isAdminAsBoolean)As SqlDataReader
'Dim getLookup As SqlDataReader = Nothing
Dim paraValueAsObject() = {eid}Dim myRoleAsString = appRole(eid)
If isAdminThen
If myRole ="A"Then
Try
Return SQLHelper.ExecuteReader("sp_locationDes", paraValue)Catch exAs Exception
Throw exEndTry
ElseIf myRole ="E"Then
Try
Return SQLHelper.ExecuteReader("sp_GetUsingCommonDB", paraValue)Catch exAs Exception
Throw exEndTry
ElseIf myRole ="B"Then
Try
Return SQLHelper.ExecuteReader("sp_getByEmpLoc", paraValue)Catch exAs Exception
Throw exEndTry
EndIf
Else
Try
Return SQLHelper.ExecuteReader("sp_GetEmpID", paraValue)Catch exAs Exception
Throw exEndTry
EndIf
End FunctionIf you indent your code to highlight the control-flow, logic errors become easier to find.
This error means that the compiler found a logic path that did not make use of a return statement.
If a function says it returns a given object type, unless it exits via an unhandled exception, it must return that object type.
If isAdminThen
If myRole ="A"Then
Return SQLHelper.ExecuteReader("sp_locationDes", paraValue)
ElseIf myRole ="E"Then
Return SQLHelper.ExecuteReader("sp_GetUsingCommonDB", paraValue)
ElseIf myRole ="B"Then
Return SQLHelper.ExecuteReader("sp_getByEmpLoc", paraValue)
EndIf
Else
Return SQLHelper.ExecuteReader("sp_GetEmpID", paraValue)
EndIf
I removed your try-catch commands for two reasons. The first reason was to make the above code snippet easier to read.
What will be returned if isAdmin is true and myRole = "NotInThisList"?
And if you say to yourself, but, "NotInThisList" isn't a valid value for myRole, remember that the compiler does not know that. :)
So, you need to add a logic path to handle some myRole value other than A, E, or B, or you need to change the ElseIf on "B" to be an Else.
The second reason I removed your try catch commands were that they were redundant. They did nothing but catch an exception and re-throw it.
If you never bothered to do that you would get the same result - an exception that needs to be handled by the calling routine. So, either add useful code that fixes the exception or logs it into your catch block, or don't bother to put them in.
Thanks Alot Its working
Glad it helped! Now you need to mark the answer so others don't wander in here to help you when you no longer need it. :)
0 comments:
Post a Comment