ma che dici, il codice funziona perfettamente :berto:
mi sono inventato la classe calculator, visto che non l'hai mandata e tutto funziona come deve essere:
codice:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="a.aspx.vb" Inherits="prove_a" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Pagina senza titolo</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label_addizione" runat="server" Text="Scegliere l'operazione da eseguire"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text="+"></asp:ListItem>
<asp:ListItem Text="-"></asp:ListItem>
<asp:ListItem Text="*"></asp:ListItem>
<asp:ListItem Text="/"></asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Campo mancante" ControlToValidate="textbox1"></asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Campo mancante" ControlToValidate="textbox2"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="Inserire valore numerico" ControlToValidate="TextBox1" Type="Integer" Operator="DataTypeCheck"></asp:CompareValidator>
<asp:CompareValidator ID="CompareValidator2" runat="server" ErrorMessage="Inserire valore numerico" ControlToValidate="textbox2" Operator="DataTypeCheck" Type="Integer"></asp:CompareValidator>
Risultato:<asp:Label ID="Label1" runat="server" Text=" "></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Calcola" />
</div>
</form>
</body>
</html>
codice:
Option Strict On
Partial Class prove_a
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = DropDownList1.SelectedValue
If DropDownList1.SelectedValue = "+" Then
Label1.Text = Calculator.Add(TextBox1.Text, TextBox2.Text)
ElseIf DropDownList1.SelectedValue = "-" Then
Label1.Text = Calculator.Subtract(TextBox1.Text, TextBox2.Text)
ElseIf DropDownList1.SelectedValue = "*" Then
Label1.Text = Calculator.Moltiplication(TextBox1.Text, TextBox2.Text)
ElseIf DropDownList1.SelectedValue = "/" Then
Label1.Text = Calculator.Division(TextBox1.Text, TextBox2.Text)
End If
End Sub
Private Class Calculator
Private Sub New()
End Sub
Public Shared Function Add(ByVal sx As String, ByVal sy As String) As String
Dim x, y As Integer
If Not (Integer.TryParse(sx, x) AndAlso Integer.TryParse(sy, y)) Then
Return "errore"
End If
Return (x + y).ToString()
End Function
Public Shared Function Subtract(ByVal sx As String, ByVal sy As String) As String
Dim x, y As Integer
If Not (Integer.TryParse(sx, x) AndAlso Integer.TryParse(sy, y)) Then
Return "errore"
End If
Return (x - y).ToString()
End Function
Public Shared Function Moltiplication(ByVal sx As String, ByVal sy As String) As String
Dim x, y As Integer
If Not (Integer.TryParse(sx, x) AndAlso Integer.TryParse(sy, y)) Then
Return "errore"
End If
Return (x * y).ToString()
End Function
Public Shared Function Division(ByVal sx As String, ByVal sy As String) As String
Dim x, y As Integer
If Not (Integer.TryParse(sx, x) AndAlso Integer.TryParse(sy, y) AndAlso y <> 0) Then
Return "errore"
End If
Return (x / y).ToString()
End Function
End Class
End Class