Visual Basic教师示例无法正常工作

时间:2012-02-21 00:26:50

标签: vb.net

刚在课堂上进行了一次演练,老师给我们的示例代码没有返回值。我目前的任务有很多麻烦,因为我没有看到它的错误。它应该取两个用户输入的数字并将它们加在一起以获得税率的总和,但它会继续返回所有0的 下面是代码的样子:

Public Class Form1

Private Sub BtnCalc_Click(ByVal sender As System.Object, _ 
 ByVal e As System.EventArgs) Handles BtnCalc.Click

    Dim amount1 As Decimal
    Dim amount2 As Decimal
    Dim myinvoice As Invoice

    myinvoice = New Invoice

    myinvoice.setlineitemamount(amount1 = Convert.ToDecimal(TextBoxAmt1.Text))
    myinvoice.setlineitemamount2(amount2 = Convert.ToDecimal(TextBoxAmt2.Text))

    LblTotal.Text = myinvoice.calculateinvoicetotal

    MessageBox.Show(myinvoice.getlineitemamount2)

    amount1 = TextBoxAmt1.Text
    amount2 = TextBoxAmt2.Text

End Sub

End Class

继承我班上的代码:

Public Class Invoice
'created new class for my invoice functions

Private lineItemamount1 As Decimal
Private lineItemamount2 As Decimal
Private lineItemamount3 As Decimal
Private lineItemamount4 As Decimal
Private invoiceDate As Date
Private customer As Integer
Private invoicetotal As Decimal
Private invoicetaxrate As Decimal = 0.1

Public Function getlineitemamount()
    Return lineItemamount1
End Function

Public Sub setlineitemamount(ByVal amount1 As Decimal)
    lineItemamount1 = amount1
End Sub

Public Function getlineitemamount2()
    Return lineItemamount2
End Function

Public Sub setlineitemamount2(ByVal amount2 As Decimal)
    lineItemamount2 = amount2
End Sub

Public Function calculateinvoicetotal()
    Return (lineItemamount1 + lineItemamount2) * invoicetaxrate
End Function

End Class

3 个答案:

答案 0 :(得分:2)

你可以尝试这个PLZ

amount1 = Convert.ToDecimal(TextBoxAmt1.Text)
myinvoice.setlineitemamount(amont1)

并且

Public Function getlineitemamount2() as decimal

当它是一个函数时,你需要指定返回值的TYPE 但我不确定这是你的问题,但值得一试

答案 1 :(得分:0)

这些行可能是问题所在:

myinvoice.setlineitemamount(amount1 = Convert.ToDecimal(TextBoxAmt1.Text))
myinvoice.setlineitemamount2(amount2 = Convert.ToDecimal(TextBoxAmt2.Text))

将它们改写为:

myinvoice.setlineitemamount(Convert.ToDecimal(TextBoxAmt1.Text))
myinvoice.setlineitemamount2(Convert.ToDecimal(TextBoxAmt2.Text))

原因是(amount1 = Convert.ToDecimal(TextBoxAmt1.Text))是内嵌比较。它将解决为真或假...

此外,我建议您启用“Option Explicit”

答案 2 :(得分:0)

您应始终将文本框的text属性指定给变量,然后将该变量传递给方法。

看一下这些视频

http://www.youtube.com/playlist?list=PL3FEE93A664B3B2E7&feature=plcp