I'm getting a warning at the end of my code that says, Warning 1 Function 'Result' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used. The code works fine, but can someone explain why I'm getting this warning and is it important?

'This code will calculate the two numbers the user inputs into the textboxes.
'
Public Class Calculator
    'this code will call AddedNum Function

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Try
            AddedNum(CInt(txtFirstNum.Text), CInt(txtSecondNum.Text), "+")
        Catch e1 As OverflowException
            MessageBox.Show("Number too large.", "Over Flow")
        Catch e2 As Exception
            MessageBox.Show("Enter a number.")
        End Try
    End Sub
    ' this code will do the calculation for the AddedNum 
    Function AddedNum(ByVal a As Integer, ByVal b As Integer, ByVal c As String) As Integer
        Dim Total As String
        Total = a + b.ToString
        txtResult.Text = Total
        Return Total = a + b.ToString
    End Function
    'This code will call the Function SubtractedNum
    Private Sub btnSubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubtract.Click
        Try
            SubtractedNum(CInt(txtFirstNum.Text), CInt(txtSecondNum.Text), "-")
        Catch e1 As OverflowException
            MessageBox.Show("Number too large.", "Over Flow")
        Catch e2 As Exception
            MessageBox.Show("Enter a number.")
        End Try

    End Sub
    'This code will do the calculation for the SubtractedNum
    Function SubtractedNum(ByVal a As Integer, ByVal b As Integer, ByVal c As String) As integer
        Dim Total As String
        Total = a - b.ToString
        txtResult.Text = Total
        Return Total = a - b.ToString
    End Function
    'This code will call the Function MultiplyNum
    Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click
        Try
            MultiplyNum(CInt(txtFirstNum.Text), CInt(txtSecondNum.Text), "*")
        Catch e1 As OverflowException
            MessageBox.Show("Number too large.", "Over Flow")
        Catch e2 As Exception
            MessageBox.Show("Enter a number.")
        End Try
    End Sub
    'This code will calculate the MultipLyNum
    Function MultiplyNum(ByVal a As Integer, ByVal b As Integer, ByVal c As String) As Integer
        Dim Total As String
        Total = a * b.ToString
        txtResult.Text = Total
        Return Total = a * b.ToString
    End Function
    'This code will call the Function DivideNum
    Private Sub btnDevide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDevide.Click
        Try
            DivideNum(CInt(txtFirstNum.Text), CInt(txtSecondNum.Text), "/")
        Catch e1 As OverflowException
            MessageBox.Show("Number too large.", "Over Flow")
        Catch e2 As DivideByZeroException
            MessageBox.Show("Error.")
        Catch e3 As Exception
            MessageBox.Show("Enter a number.")
        End Try
    End Sub
    'This code will calculate DivideNum
    Function DivideNum(ByVal a As Integer, ByVal b As Integer, ByVal c As String) As Integer
        Dim Total As String
        Total = a / b.ToString
        txtResult.Text = Total
        Return Total = a / b.ToString
    End Function
    'This code will call the PowNum Function
    Private Sub btnPow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPow.Click
        Try
            PowNum(CInt(txtFirstNum.Text), CInt(txtSecondNum.Text), "^")
        Catch e1 As OverflowException
            MessageBox.Show("Number too large.", "Over Flow")
        Catch e2 As Exception
            MessageBox.Show("Enter a number.")
        End Try
    End Sub
    'This code will calculate the PowNum
    Function PowNum(ByVal a As Integer, ByVal b As Integer, ByVal c As String) As Integer
        Dim Total As String
        Total = a ^ b.ToString
        txtResult.Text = Total
        Return Total = a ^ b.ToString
    End Function
    'This code will clear the textboxes
    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        txtFirstNum.Clear()
        txtSecondNum.Clear()
        txtResult.Clear()

    End Sub
    Function Result(ByVal a As Integer, ByVal b As Integer, ByVal c As String)

        If c = "+" Then
            Return a + b
        End If
        If c = "-" Then
            Return a - b
        End If
        If c = "*" Then
            Return a * b
        End If
        If c = "/" Then
            Return a / b
        End If
        If c = "^" Then
            Return a ^ b
        End If
    End Function

End Class

Recommended Answers

All 2 Replies

A couple of things I noticed. Several of your functions are supposed to return an Integer and your returning a String. This is a sloppy habit to get into and can create huge problems in a large project. One way to guard against this is to set Option Strict to On. In fact it's usually a good idea to set all the Options(Explicit, Strict, Infer) to On. This helps to correct bad habits before they develop.

As for the function with the warning. Result only returns a value inside the if statements and if c is passed with the wrong string no return statment will be executed. Here's one way around this:

Function Result(ByVal a As Integer, ByVal b As Integer, ByVal c As String) As Double
    Result = 0
    Select Case c
        Case "+"
            Result = a + b
        Case "-"
            Result = a - b
        Case "*"
            Result = a * b
        Case "/"
            Result = a / b
        Case "^"
            Result = a ^ b                
    End Select
End Function

By giving Result a default value the compiler knows that it will always get a value.

You might find the following helpful:

Public Class Calc1

    Function Result(op As Operations) As String
        Try
            Dim Total As String = ""
            Dim a, b As Integer
            If Integer.TryParse(txtFirstNum.Text, a) AndAlso Integer.TryParse(txtSecondNum.Text, b) Then
                Select Case op
                    Case Operations.Add
                        Total = (a + b).ToString
                    Case Operations.Divide
                        Total = (a / b).ToString
                    Case Operations.Multiply
                        Total = (a * b).ToString
                    Case Operations.Subtract
                        Total = (a - b).ToString
                    Case Operations.Power
                        Total = (a ^ b).ToString
                End Select
                txtFirstNum.Clear()
                txtSecondNum.Clear()
            Else
                MessageBox.Show("Invalid input")
            End If
        Catch e1 As OverflowException
            MessageBox.Show("Number too large.", "Over Flow")
        End Try
    End Sub
    Enum Operations
        Add
        Subtract
        Multiply
        Divide
        Power
        Clear
    End Enum
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        txtTotal.txt = Result(Operations.Add)
    End Sub
    Private Sub btnSubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubtract.Click
        txtTotal.txt = Result(Operations.Subtract)
    End Sub
    Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click
        txtTotal.txt = Result(Operations.Multiply)
    End Sub
    Private Sub btnDevide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.Click
        txtTotal.txt = Result(Operations.Divide)
    End Sub
    Private Sub btnPow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPow.Click
        txtTotal.txt = Result(Operations.Power)
    End Sub
    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        txtTotal.txt = Result(Operations.Clear)
    End Sub
End Class

Once again good sir, thank you!!! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.