• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 361
  • Last Modified:

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

0
kevin33809
Asked:
kevin33809
1 Solution
 
G_Hosa_PhatCommented:
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.
0
 
Imran Javed ZiaConsultant Software Engineer - .NET ArchitectCommented:
Hi,
Please change it as one


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)
            Throw
        End Try

    End Function  <-- the error occurs here.

Open in new window

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

0
 
kevin33809Author Commented:
Even more verbage about the THROW clause would have been very helpful, the solution was perfect.  Thanks IJZ
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now