kevin33809
asked on
VS2008 - Warning messages appears when I run program. How do I fix them?
Hello
I new to working with VS2008. My company wants me to debug a program in VS2008 coded in Visual Basic. When I run the program in Debug mode I get the following warning messages below. I've been trying to fix them based on what I know from Visual Basic 6, but now I'm sure in regards to coding in Visual Basic in VS2008. How do I fix these issue, and why.
========
See below the warning messages, and see attached file for code.
Warning 1 - Function 'OraConnection' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used. C:\Development\CPR_Env_Mai ntenance\C PR_APPLICA TIONINFO\D ata Source\DBConnection.vb 61 5 CprApplicationInfo
Warning 2 - Function 'Connection' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used. C:\Development\CPR_Env_Mai ntenance\C PR_APPLICA TIONINFO\D ata Source\DBConnection.vb 115 5 CprApplicationInfo
I new to working with VS2008. My company wants me to debug a program in VS2008 coded in Visual Basic. When I run the program in Debug mode I get the following warning messages below. I've been trying to fix them based on what I know from Visual Basic 6, but now I'm sure in regards to coding in Visual Basic in VS2008. How do I fix these issue, and why.
========
See below the warning messages, and see attached file for code.
Warning 1 - Function 'OraConnection' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used. C:\Development\CPR_Env_Mai
Warning 2 - Function 'Connection' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used. C:\Development\CPR_Env_Mai
Public Shared Function OraConnection() As OracleClient.OracleConnection
Try
CN = New OracleClient.OracleConnection
CN.ConnectionString = sConnBase 'cConnString_BASE
CN.Open()
Return CN
Catch Ex As Exception
MessageBox.Show("Application could not connect to the Database." & ControlChars.NewLine & ControlChars.NewLine & Ex.Message, "Connection to Database", MessageBoxButtons.OK, MessageBoxIcon.Stop, MessageBoxDefaultButton.Button1)
End Try
End Function <-- the error occurs here.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Even more verbage about the THROW clause would have been very helpful, the solution was perfect. Thanks IJZ
The reason for THIS particular warning is that, if an exception occurs in your Try block before it gets to the line "Return CN", the application will immediately jump to the Catch block, won't hit that Return line, and therefore won't return a valid value to the calling function. The easiest fix would be simply to put one more statement in your Catch block:
Open in new window
This way the application can return a valid value of "Nothing" to the calling function, rather than not returning anything at all.