Link to home
Create AccountLog in
Avatar of kevin33809
kevin33809Flag for United States of America

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_Maintenance\CPR_APPLICATIONINFO\Data 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_Maintenance\CPR_APPLICATIONINFO\Data Source\DBConnection.vb      115      5      CprApplicationInfo

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.

Open in new window

Avatar of G_Hosa_Phat
G_Hosa_Phat
Flag of United States of America image

These warnings are just that - Warnings.  They don't necessarily mean there's a problem, but the VS IDE knows enough to let you know that it could result in a problem under certain circumstances.

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:
    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)
            Return Nothing
        End Try

    End Function  <-- the error occurs here. 

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.
ASKER CERTIFIED SOLUTION
Avatar of Imran Javed Zia
Imran Javed Zia
Flag of Pakistan image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of kevin33809

ASKER

Even more verbage about the THROW clause would have been very helpful, the solution was perfect.  Thanks IJZ