Vikas Ahlawat

Vikas Ahlawat

  • NA
  • 564
  • 809.6k

C# Error: not all code paths return a value?

Sep 28 2010 6:27 AM
I have converted a VB.net prj to C# and now removing error manually.
I have got this : error not all code paths return a value

The code is here

        public double Evaluate(string strInput)
        {
            try
            {
               
               
                int a;
                int b;
                int i;
                double middle;
                double result;
                string[] plus;
                string[] minus;
                string[] mult;
                string[] div;
                string[] pow;
               
               
               
                if (strInput.IndexOf("(") != - 1)
                {
                    a = strInput.LastIndexOf("(");
                    b = strInput.IndexOf(")", a);
                    middle = Evaluate(strInput.Substring(a + 1, b - a - 1));
                    return Evaluate(strInput.Substring(0, a) + middle.ToString() + strInput.Substring(b + 1));
                }
                result = 0;
              
                plus = strInput.Split('+');
               
                if (plus.Length > 1)
                {
                    // there were some +
                    result = Evaluate(plus[0]);
                   
                    for (i = 1; i <= plus.Length - 1; i++)
                    {
                        result += Evaluate(plus[i]);
                    }
                    return result;
                   
                }
                else
                {
                    // no +
                    minus = plus[0].Split('-');
                    if (minus.Length > 1)
                    {
                        //there were some -
                        result = Evaluate(minus[0]);
                        for (i = 1; i <= minus.Length - 1; i++)
                        {
                            result -= Evaluate(minus[i]);
                        }
                        return result;
                       
                    }
                    else
                    {
                        // no -
                        mult = minus[0].Split('*');
                        if (mult.Length > 1)
                        {
                            // there were some *
                            result = Evaluate(mult[0]);
                            for (i = 1; i <= mult.Length - 1; i++)
                            {
                                result *= Evaluate(mult[i]);
                            }
                            return result;
                        }
                        else
                        {
                            // no *
                            div = mult[0].Split('/');
                            if (div.Length > 1)
                            {
                                // there were some /
                                result = Evaluate(div[0]);
                                for (i = 1; i <= div.Length - 1; i++)
                                {
                                    result /= Evaluate(div[i]);
                                }
                                return result;
                               
                            }
                            else
                            {
                                // no /
                                pow = div[0].Split('^');
                                if (pow.Length > 1)
                                {
                                    // there were some ^
                                    result = Evaluate(pow[0]);
                                    for (i = 1; i <= pow.Length - 1; i++)
                                    {
                                        result = Math.Pow(result, Evaluate(pow[i])) ;
                                    }
                                    return result;
                                }
                                else
                                {
                                    // no ^
                                    return double.Parse(strInput);
                                }
                               
                            }
                        }
                      
                    }
                   
                }

               
            }
            catch (Exception ex)
            {
                CError.ShowError(ex , true, "","");
            }
          
        }







and the VB.net Code is here in which no error

 Public Function Evaluate(ByVal strInput As String) As Double
        Try

       
            Dim a, b, i As Integer
            Dim middle, result As Double
            Dim plus, minus, mult, div, pow As String()



            If (strInput.IndexOf("(") <> -1) Then
                a = strInput.LastIndexOf("(")
                b = strInput.IndexOf(")", a)
                middle = Evaluate(strInput.Substring(a + 1, b - a - 1))
                Return Evaluate(strInput.Substring(0, a) + middle.ToString() + strInput.Substring(b + 1))
            End If
            result = 0
            plus = strInput.Split("+")

            If (plus.Length > 1) Then
                ' there were some +
                result = Evaluate(plus(0))

                For i = 1 To plus.Length - 1
                    result += Evaluate(plus(i))
                Next
                Return result

            Else
                ' no +
                minus = plus(0).Split("-")
                If (minus.Length > 1) Then
                    'there were some -
                    result = Evaluate(minus(0))
                    For i = 1 To minus.Length - 1
                        result -= Evaluate(minus(i))
                    Next
                    Return result

                Else
                    ' no -
                    mult = minus(0).Split("*")
                    If (mult.Length > 1) Then
                        ' there were some *
                        result = Evaluate(mult(0))
                        For i = 1 To mult.Length - 1
                            result *= Evaluate(mult(i))
                        Next
                        Return result
                    Else
                        ' no *
                        div = mult(0).Split("/")
                        If (div.Length > 1) Then
                            ' there were some /
                            result = Evaluate(div(0))
                            For i = 1 To div.Length - 1
                                result /= Evaluate(div(i))
                            Next
                            Return result

                        Else
                            ' no /
                            pow = div(0).Split("^")
                            If (pow.Length > 1) Then
                                ' there were some ^
                                result = Evaluate(pow(i))
                                For i = 1 To pow.Length - 1
                                    result ^= Evaluate(pow(i))
                                Next
                                Return result
                            Else
                                ' no ^
                                Return Double.Parse(strInput)
                            End If

                        End If
                    End If
                End If
            End If

        Catch ex As Exception
            CError.ShowError(ex, True, False)
        End Try

    End Function

Answers (2)