Tuesday, February 22, 2011

Tuesday, February 22 2011





<asp:TextBox ID="txtValue1" runat="server"></asp:TextBox>
<asp:DropDownList ID="firstOperator" runat="server">
<asp:ListItem>+</asp:ListItem>
<asp:ListItem>-</asp:ListItem>
<asp:ListItem>*</asp:ListItem>
<asp:ListItem>/</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="txtValue2" runat="server"></asp:TextBox>
<br />
<asp:Label ID="lblResult" runat="server"></asp:Label>
<br />
<asp:Button ID="btnCalculate" runat="server" Text="Calculate" /></asp:Label>









Public Class Calculator

Public Function Add(ByVal a As Double, ByVal b As Double) As Double
Return a + b
End Function

Public Function Subtract(ByVal a As Double, ByVal b As Double) As Double
Return a - b
End Function

Public Function Multiply(ByVal a As Double, ByVal b As Double) As Double
Return a * b
End Function

Public Function Divide(ByVal a As Double, ByVal b As Double) As Double
Return a / b
End Function

End Class

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub firstOperator_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles firstOperator.SelectedIndexChanged
If txtValue1.Text.Length > 0 AndAlso txtValue2.Text.Length > 0 Then

Dim result As Double = 0
Dim value1 As Double = Convert.ToDouble(txtValue1.Text)
Dim value2 As Double = Convert.ToDouble(txtValue2.Text)
Dim myCalculator As New Calculator()

Select Case firstOperator.SelectedValue
Case "+"
result = myCalculator.Add(value1, value2)
Case "-"
result = myCalculator.Subtract(value1, value2)
Case "*"
result = myCalculator.Multiply(value1, value2)
Case "/"
result = myCalculator.Divide(value1, value2)
End Select
lblResult.Text = result.ToString()
Else
lblResult.Text = String.Empty
End If
End Sub
End Class

No comments: