I'm having trouble with this part.My professor wants us to do this, when each button is clicked it 'gathers' the input and calls a sub named Result. I'm guessing we're suppose to use a Sub names Result that will call the other sub procedures.
this is what I have so far.
Can some one tell me how to do this?

 Public Class Calculator
        Sub Result(ByVal a As Integer, ByVal b As Integer, ByVal op As String)



    End Sub

    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

    Sub AddedNum(ByVal a As Integer, ByVal b As Integer)
        Dim Total As String
        Total = a + b.ToString
        MessageBox.Show("Your total is " & Total.ToString, "Calculation", MessageBoxButtons.OK)
        If Windows.Forms.DialogResult.OK Then
            txtFirstNum.Clear()
            txtSecondNum.Clear()
        End If

    End Sub
    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
    Sub SubtractedNum(ByVal a As Integer, ByVal b As Integer)
        Dim Total As String
        Total = a - b.ToString
        MessageBox.Show("Your total is " & Total.ToString, "Calculation", MessageBoxButtons.OK)
        If Windows.Forms.DialogResult.OK Then
            txtFirstNum.Clear()
            txtSecondNum.Clear()
        End If
    End Sub

    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
    Sub MultiplyNum(ByVal a As Integer, ByVal b As Integer)
        Dim Total As String
        Total = a * b.ToString
        MessageBox.Show("Your total is " & Total.ToString, "Calculation", MessageBoxButtons.OK)
        If Windows.Forms.DialogResult.OK Then
            txtFirstNum.Clear()
            txtSecondNum.Clear()
        End If
    End Sub

    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
    Sub DivideNum(ByVal a As Integer, ByVal b As Integer)
        Dim Total As String
        Total = a / b.ToString
        MessageBox.Show("Your total is " & Total.ToString, "Calculation", MessageBoxButtons.OK)
        If Windows.Forms.DialogResult.OK Then
            txtFirstNum.Clear()
            txtSecondNum.Clear()
        End If
    End Sub

    Private Sub btnPow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPow.Click
        Try
            PowNum(CInt(txtFirstNum.Text), CInt(txtFirstNum.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
    Sub PowNum(ByVal a As Integer, ByVal b As Integer)
        Dim Total As String
        Total = a ^ b.ToString
        MessageBox.Show("Your total is " & Total.ToString, "Calculation", MessageBoxButtons.OK)
        If Windows.Forms.DialogResult.OK Then
            txtFirstNum.Clear()
            txtSecondNum.Clear()
        End If
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        txtFirstNum.Clear()
        txtSecondNum.Clear()

    End Sub



End Class

Recommended Answers

All 6 Replies

What he most likely means is that each button will pass both operands and a code for the operation required to a sub routine named results that will carry out the required operation and output the result. If you look at your code you'll see how there are several blocks of code that are the same or similar between the different operations. Putting these into one sub routine should allow you to eliminate copying these blocks for each operation. A hint would be to use a number code to represent the operation required, then a Select-Case block to perform it.

I'm not sure exactly what you're asking, but if you wanted to click a button with a sub procedure or function or another event handler, or whatever, then you need to use the Object.PerformClick() procedure. Let's say that you have a button called btnStart and you want it to click when your form loads. In your form load event handler you would place this--> btnStart.PerformClick()

I hope that helps.

@tinstaafl can you show me an example of this?

One of the easiest ways to implement this is with an Enum. An enum is bascally a list of constants. The thing about an enum is each constant is identified by a name. This makes things much easier to read. For instance with an Enum like this:

Enum Operations
    Add
    Subtract
    Multiply
    Divide
    Power
    Clear
End Enum

You identify the operation you need to perform by its name(Operations.Add)

If you make this the argument for your sub routine it would look something like this:

Sub Result(op As Operations)

End Sub

to call it from the button click handler would look like this:

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
    Result(Operations.Add)
End Sub

Inside Result use a Try block still but only for the overflow exception. For input errors use the Integer.TryParse. This evaluates the string and returns true if it's an integer and set the value of the string to an integer variable. Because the return is a boolean it can beused inside a conditional statement:

        Dim Total As String = ""
        Dim a, b As Integer
        If Integer.TryParse(txtFirstNum.Text, a) AndAlso Integer.TryParse(txtSecondNum.Text, b) Then

        End If

Inside this block put the Select Case block.

            Select Case op
                Case Operations.Add
                    Total = (a + b).ToString
                Case Operations.Divide
                    Total = (a / b).ToString
                '... and so on

            End Select

If you're not allowed to use an Enum use strings,Case "Add" instead.

Hopefully this is enough for you to piece it together.

Wow! Thank you so much. This works.

You're very welcome. Please remeber to mark this solved. Thanks.

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.