无法访问课程中的文本框输入

时间:2012-02-20 03:49:16

标签: vb.net

我是VB的新手,并且在课堂作业方面遇到了一些麻烦。基本上它应该采取发票小计 - 找到折扣百分比并给出实际总数。我似乎无法让我的类访问用户输入的文本框,它只是继续返回0。这也是一个家庭作业...我不是只是要求答案的一些提示,因为我真的不知所措,无法弄清楚这一点!!!

这是我点击按钮事件的代码:

Public Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
    'If txbxSubtotal.Text >= 500 Then
    '   discountpercent = 0.2
    'ElseIf txbxSubtotal.Text >= 250 And txbxSubtotal.Text < 500 Then
    '   discountpercent = 0.15
    'ElseIf txbxSubtotal.Text >= 100 And txbxSubtotal.Text < 250 Then
    '   discountpercent = 0.1
    'Else
    '   discountpercent = 0
    'End If
    'discountAmount = txbxSubtotal.Text * discountpercent
    'total = txbxSubtotal.Text - discountAmount
    Dim myinvoice As New Getinvoice
    myinvoice.setAmount(total = Convert.ToDecimal(txbxSubtotal.Text))
    total = txbxSubtotal.Text
    MessageBox.Show(myinvoice.getdiscountAmount)
    MessageBox.Show(myinvoice.getAmount)
End Sub
End Class

这是我班上的代码:

Public Class Getinvoice

    Private subtotal As Decimal
    Private discount As Decimal
    Private discountAmount As Decimal
    Private discountpercent As Decimal
    Private amount As Decimal

    Public Sub setAmount(ByVal total As Decimal)
        amount = total
    End Sub

    Public Function getAmount()
        Return amount
    End Function

    Public Sub setdiscountPercent(ByVal discPcnt As Decimal)
        discountAmount = discPcnt

        If amount >= 500 Then
            discountpercent = 0.2
        ElseIf amount >= 250 And amount < 500 Then
            discountpercent = 0.15
        ElseIf amount >= 100 And amount < 250 Then
            discountpercent = 0.1
        Else
            discountpercent = 0
        End If
    End Sub

    Public Function getdiscountAmount()
        Return discountpercent
    End Function

End Class

1 个答案:

答案 0 :(得分:0)

您从未致电

setdiscountPercent(ByVal discPcnt As Decimal)

所以从不设置discountpercent。这就是为什么你得到0;

编辑:

Public Sub setdiscountPercent(ByVal discPcnt As Decimal)
    discountAmount = discPcnt

    If amount >= 500 Then
        discountpercent = 0.2
    ElseIf amount >= 250 And amount < 500 Then
        discountpercent = 0.15
    ElseIf amount >= 100 And amount < 250 Then
        discountpercent = 0.1
    Else
        discountpercent = 0
    End If
End Sub

这是你的代码。看一下您在此方法discountAmount = discPcnt

中设置的内容

但是,在您发布的代码中,您永远不会致电setdiscountPercent(ByVal discPcnt As Decimal)

因此,discountAmount和discountpercent永远不会设置为任何值。这就是问题的来源。