Вопрос

The program is supposed to use the enumeration type declared at the beginning within the Select Case statement. When I run the program, no matter which operator I select, it only uses the first case statement (for add). I tried rearranging them in the case statement to see if it just used the first one, but it still just chose add. What am I doing wrong?

Public Class Assignment2Calculator

    Dim Operand1 As Decimal
    Dim Operand2 As Decimal
    Dim CalcOperator As Integer

    Enum OperatorEnum
        add
        divide
        multiply
        subtract
    End Enum

    Private Sub Assignment2Calculator_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        cboOperator.DataSource = [Enum].GetValues(GetType(OperatorEnum))
        AcceptButton = btnCalculate

    End Sub

    Private Sub IsValidData(ByVal txtOperand1 As Object, ByVal txtOperand2 As Object)
        Operand1 = Convert.ToDecimal(txtOperand1.Text)
        Operand2 = Convert.ToDecimal(txtOperand2.Text)

        If (Operand1 > 0 Or Operand1 < 1000000) And (Operand2 > 0 Or Operand2 < 1000000) Then
            If IsNumeric(Operand1) And IsNumeric(Operand2) Then

            End If
        Else
            MessageBox.Show("Operand 1 or 2 is not valid")
        End If

    End Sub

    Private Sub btnCalculate_Click(sender As System.Object, e As System.EventArgs) Handles btnCalculate.Click

        IsValidData(txtOperand1, txtOperand2)


        Calculate(Operand1, Operand2, CalcOperator)


    End Sub


    Private Sub Calculate(ByVal Operand1 As Decimal, ByVal Operand2 As Decimal, _
                          ByVal CalcOperator As Integer)
        'Performs requested operation using select case and enumeration

        Dim answer As Decimal
        Dim CalcOperatorCase As OperatorEnum

        Select Case CalcOperatorCase
            Case Is = OperatorEnum.add

                answer = Operand1 + Operand2
                txtResult.Text = FormatNumber(answer, 3)

            Case Is = OperatorEnum.divide
                answer = Operand1 - Operand2
                txtResult.Text = FormatNumber(answer, 3)

            Case Is = OperatorEnum.multiply
                answer = Operand1 * Operand2
                txtResult.Text = FormatNumber(answer, 3)

            Case Is = OperatorEnum.subtract
                answer = Operand1 / Operand2
                txtResult.Text = FormatNumber(answer, 3)

            Case Else
                MessageBox.Show("Please select an operator")

        End Select

    End Sub


End Class 
Это было полезно?

Решение

You never assign CalcOperator, so it always remains the default for an Integer, which is 0 - which is add in your enum.

You can either add an event to change it whenever your ComboBox changes, or you can do it on btnCalculate_Click. If you don't need it anywhere else, you might as well do it on the Click event:

CalcOperator = CType(cboOperator.SelectedItem, OperatorEnum)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top