• Instruction: Bonus

    Develop a calculator application that will allow the user to enter the desired calculation (A for Addition, S for Subtraction) and enter two numbers.
    When “Calculate” button is clicked, if the user entered two numbers, the application will display the appropriate result. Otherwise, a message box will show “You did not enter two numbers”, then quit the application. (Hint: use function IsNumeric)

  • Question

Is there any better way I could haven't written this? Did I write the code correct? What other way I can write this?

   Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
        Dim Num1 As String
        Dim Num2 As String
        Dim AorS As String
        Dim result As String

        Num1 = ((txtFirst.Text))
        Num2 = (txtSecond.Text)
        AorS = txtEnter.Text.ToUpper
        result = txtResult.Text

        If IsNumeric(Num1) = True And
            IsNumeric(Num2) = True And
            AorS = "A" Then
            result = CStr(CDbl(Num1) + CDbl(Num2))
        End If


        If IsNumeric(Num1) = True And
            IsNumeric(Num2) = True And
            AorS = "S" Then
            result = CStr((CDbl(Num1) - CDbl(Num2)))
        End If

        If IsNumeric(Num1) = False Or
               IsNumeric(Num2) = False Or
               AorS = "" Then
            MessageBox.Show(" You Did Not Enter A Valid Entry! ")

        End If

You tried to write it, but your direction of thinking is not in proper way.
What did you try to do, was not in your mind.
What were your jobs?

Declaring varibles: You did it properly.

Dim Num1 As String
Dim Num2 As String
Dim AorS As String
Dim result As String

Asigning values to the variables: You did it nearabout properly.

 Num1 = ((txtFirst.Text))
 Num2 = (txtSecond.Text)
 AorS = txtEnter.Text.ToUpper
 result = txtResult.Text

result = txtResult.Text, you did this needlessly. This was not the proper position to asign the value for the variable "result".

Calculation part: Not so good. You need more thinkig about this part.

Display the calculated result: Everything you lost here. Where did you try to show your result. I found your result nowhere.

You need some modification.
The following codes show here how can you do more efficiently.

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'initializing the textboxes
        txtFirst.Text = ""
        txtSecond.Text = ""
        txtEnter.Text = ""

        txtEnter.MaxLength = 1

    End Sub

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



        If Not TextValidation() Then
            'nothing to do
            Exit Sub
        End If

        'Calculate as per operator and display the result
        Select Case txtEnter.Text.ToUpper
            Case "A"
                txtResult.Text = Val(txtFirst.Text) + Val(txtSecond.Text)
            Case "S"
                txtResult.Text = Val(txtFirst.Text) - Val(txtSecond.Text)
            Case Else
                MessageBox.Show(String.Format("You choose wrong operator.{0}Choose any one of '{1}''{2}' or '{3}''{4}'", vbCrLf, "A", "a", "B", "b"))
        End Select


    End Sub

    Private Function TextValidation() As Boolean
        'Checking if first number is empty or not
        If String.IsNullOrWhiteSpace(txtFirst.Text) Then
            MessageBox.Show("First number is empty.")
            txtFirst.Focus()
            Return False
        End If

        'Checking if Operator if empty or not
        If String.IsNullOrWhiteSpace(txtEnter.Text) Then
            MessageBox.Show("Operator is empty.")
            txtEnter.Focus()
            Return False
        End If

        'checking second number is empty or not
        If String.IsNullOrWhiteSpace(txtSecond.Text) Then
            MessageBox.Show("Second number is empty.")
            txtSecond.Focus()
            Return False
        End If


        'If All ok ready to calculate
        Return True
    End Function


End Class

You did pretty good. But, I would have simplified it by validating up front. That way you are not executing needless code.

Make Num1 & Num2 numeric - easier to code

 Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Dim Num1 As Double
Dim Num2 As Double
Dim AorS As String
Dim result As String

if NOT IsNumeric(txtFirst.Text) then
    MessageBox.Show(" You Did Not Enter A Number! ")
    txtFirst.Focus
    exit sub
end If

if NOT IsNumeric(txtSecond.Text) then
    MessageBox.Show(" You Did Not Enter A Number! ")
    txtSecond.Focus
    exit sub
end If

'Now, all data is valid.  Simply do the addition or subtraction
Num1 = CDbl(txtFirst.Text)
Num2 = CDbl(txtSecond.Text)


AorS = txtEnter.Text.ToUpper

If AorS = "A" Then
    result = CStr(Num1 + Num2)
else
    result = CStr(Num1 - Num2)
End If

txtResult.Text=result
end sub

I assumed AorS could only be "A" or "S". If you have to validate that as well, put another validation if/end block in as well.

I just wanted to show you that validating up front can reduce your coding.

Good luck!

Jerry

This article has been dead for over six months. Start a new discussion instead.