locked
Expression does not produce a value error RRS feed

  • Question

  •    

    I'm confused as to why my Convert.ToDecimal statements all return expression does not produce a value errors.  I am new to VB but I have used similar constructs in other programs.

    Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click


            Dim decCommissionRate As Decimal = 0.07D
            Dim decAverageSalesCommission As Decimal
            Dim decTotalSalesAmount As Decimal
            Dim decTotalCommissionPaid As Decimal



            If IsNumeric(txtEmp1.Text) And _
                IsNumeric(txtEmp2.Text) And _
                IsNumeric(txtEmp3.Text) And _
                IsNumeric(txtEmp4.Text) And _
                IsNumeric(txtEmp5.Text) And _
                IsNumeric(txtEmp6.Text) Then


                decTotalSalesAmount = Convert.ToDecimal(txtEmp1.Text) + _
                Convert.ToDecimal(txtEmp2.Text) + _
                Convert.ToDecimal(txtEmp3.Text) + _
                Convert.ToDecimal(txtEmp4.Text) + _
                Convert.ToDecimal(txtEmp5.Text) + _
                Convert.ToDecimal(txtEmp6.Text)

                decAverageSalesCommission = CLng(decTotalSalesAmount) \ 6

                decTotalCommissionPaid = CLng(decTotalSalesAmount) \ CLng(decCommissionRate)

    Sunday, March 9, 2014 8:37 PM

Answers

  • When I went to Go To Definition, it took me to the Public Sub Convert()

    That is the problem.   You should disambiguate the reference by providing a fully qualified path:

                decTotalSalesAmount = System.Convert.ToDecimal(txtEmp1.Text) + _

    or remove the reference to that other library, if it's not being used.

    Sunday, March 9, 2014 10:16 PM

All replies

  • The operator '\' returns an integer while '/' return the type of number baser on the input and output variables in the line of code.

    jdweng

    Sunday, March 9, 2014 8:58 PM
  •    

    I'm confused as to why my Convert.ToDecimal statements all return expression does not produce a value errors.  I am new to VB but I have used similar constructs in other programs.

    There is nothing in that code that would produce the error you have quoted.   Select Convert.ToDecimal in your code and right click and choose Go To Definition to confirm that you are accessing the function that you think you are. Simplify the expression and see if the error persists or is specific to one of the textboxes.

                decTotalSalesAmount = Convert.ToDecimal(txtEmp1.Text)
                decTotalSalesAmount += Convert.ToDecimal(txtEmp2.Text)
                decTotalSalesAmount += Convert.ToDecimal(txtEmp3.Text)
                decTotalSalesAmount += Convert.ToDecimal(txtEmp4.Text)
                decTotalSalesAmount += Convert.ToDecimal(txtEmp5.Text)
                decTotalSalesAmount += Convert.ToDecimal(txtEmp6.Text)

    Note that the conversion to Long for the calculation is not required, and you should not be using integer divide for those calculations.

        decAverageSalesCommission = decTotalSalesAmount / 6
        decTotalCommissionPaid = decTotalSalesAmount / decCommissionRate
    

    Sunday, March 9, 2014 9:04 PM
  • I had the wrong slash for my division operator.  I corrected that.

    When I went to Go To Definition, it took me to the Public Sub Convert()


    Sunday, March 9, 2014 10:02 PM
  • This is the error I see in VS...

    Sunday, March 9, 2014 10:03 PM
  • When I went to Go To Definition, it took me to the Public Sub Convert()

    That is the problem.   You should disambiguate the reference by providing a fully qualified path:

                decTotalSalesAmount = System.Convert.ToDecimal(txtEmp1.Text) + _

    or remove the reference to that other library, if it's not being used.

    Sunday, March 9, 2014 10:16 PM
  • System.Convert did the trick!  Thanks so much!
    Monday, March 10, 2014 7:53 PM