我在vb.net上工作,我正在做的基本事情是存款和提取金额

时间:2018-05-30 16:15:55

标签: vb.net

So  If txtFirstName.Text = String.Empty And
        txtLastName.Text = String.Empty And
        txtAmount.Text = String.Empty Then
    MsgBox("Please enter Information")

这部分根本不起作用,当我试着保留断点并看看它是如何工作时,它出来了,但msgbox从未显示过。 存款和取款功能作为字符串。 此外,我想知道如何将我的代码保存在不同的类中,并在此类中使用它来存储和撤销按钮。 这是我写的代码:

    Option Strict On
    Option Explicit On

    Public Class _Default
        Inherits System.Web.UI.Page

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As 
System.EventArgs) Handles Me.Load
           if not IsPostBack then
             lblFirstName.Focus()
        End Sub

        Protected Sub btnConfirm_click(ByVal sender As Object, ByVal e As 
System.EventArgs) Handles btnConfirm.Click
            If txtFirstName.Text = String.Empty And
                    txtLastName.Text = String.Empty And
                    txtAmount.Text = String.Empty Then
                MsgBox("Please enter Information")
            Else
                doConfirm()
            End If
        End Sub

        Private Sub doConfirm()
            If rbtndeposit.Checked Then
                txtBalance.Text += (txtAmount.Text)
            ElseIf rbtnWithdraw.Checked Then
              if txtAmount.text <= txtBalance.text then
                txtBalance.text -= txtAmount.text
             else
                msgBox("Funds not sufficient") 
            End If
            End If

        End Sub
    End Class

1 个答案:

答案 0 :(得分:0)

首先,你的文字输入逻辑有点缺陷,AND应该是OrElse:

Protected Sub btnConfirm_click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConfirm.Click

        If txtFirstName.Text.Length < 1 OrElse _
                txtLastName.Text.Length < 1 OrElse _
                txtAmount.Text.Length < 1 Then

            MsgBox("Please enter Information")

        Else

            doConfirm()

        End If

    End Sub

其次,您正在尝试使用文本值进行数学计算。在进行计算时将输入转换为正确的数据类型。

    Private Sub doConfirm()

        If rbtndeposit.Checked Then

           txtBalance.Text = Cstr(Cdbl(txtBalance.Text) + Cdbl(txtAmount.Text))

        ElseIf rbtnWithdraw.Checked Then

            If CDbl(txtAmount.text) <= Cdbl(txtBalance.text) Then

               txtBalance.text = Cstr(Cdbl(txtBalance.text) - Cdbl(txtAmount.text))

            Else

                msgBox("Funds not sufficient") 

            End If

        End If

    End Sub

您应该如何使用tryparse来检查金额的输入是否有效,或者当输入无效时您的代码会崩溃,例如:

 Dim amount As Double

 If Double.TryParse(txtAmount.text, amount) Then

    If rbtndeposit.Checked Then

        txtBalance.Text = Cstr(Cdbl(txtBalance.Text) + amount)

    ElseIf rbtnWithdraw.Checked Then

        If amount <= Cdbl(txtBalance.text) then

            txtBalance.text = Cstr(Cdbl(txtBalance.text) - amount)

        else

            msgBox("Funds not sufficient") 

        End If

    End If

 Else

   MessageBox.Show("Invalid amount entered.")


 End if

至于存储你的程序是一个单独的类,我认为只需创建函数就足够了:

Private Sub Button1_WithDraw(sender As Object, e As EventArgs) Handles Button1_WithDraw.Click

    txtBalance.Text = Withdraw(CDbl(txtAmount.Text), Cdbl(txtBalance.text)).ToString

End Sub

Private Function Withdraw(ByVal Amount As Double, ByVal Balance As Double) As Double

    Balance = Balance - Amount

    Return Balance

End Function