在Visual Basic中使用模块执行计算

时间:2014-03-07 03:53:27

标签: vb.net module

我正在为我的Visual Basic类编写一个程序,该程序应该能够计算从一个列表框中选择并添加到另一个列表框中的项目的最终总价。其中2项具有必须添加到最终价格中的销售税。该计划还有一个模块,该模块应该用于记录所有税收,并用于执行所有与税收相关的功能。

这是最新的代码。

Option Strict On
Public Class Form1

Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAdd.Click
    If (txtQuantity.Text = "") Then
        MessageBox.Show("Please enter a quantity for the item you selected")
    ElseIf Not Integer.TryParse(txtQuantity.Text, CInt(txtQuantity.Text)) Then
        MessageBox.Show("The quantity entered is not numeric. Please add a numeric quantity.")
        Exit Sub
    Else
        lstPurchased.Items.Add(txtQuantity.Text & " " & lstSale.Text)
    End If
End Sub

Private Sub btnCalculate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCalculate.Click
    Dim int As Integer
    Dim total As Double = 0

    For i As Integer = 0 To lstPurchased.Items.Count - 1
        Dim lst() As String = lstPurchased.Items(i).ToString.Split({CChar(" ")}, 2)
        Integer.TryParse(lst(0), int)
        total += TaxesModule.SalesTax(int, lst(1))
    Next
    MessageBox.Show(CStr(FormatCurrency(total)))
End Sub

Private Sub btnClear_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnClear.Click
    lstPurchased.Items.Clear()
    txtQuantity.Clear()
End Sub

End Class

最新的模块代码

Option Strict On

Module TaxesModule
Private Const DONUT_TAX As Decimal = CDec(0.083)
Private Const RAISIN_TAX As Decimal = CDec(0.02)
Private Const SS_TAX As Decimal = CDec(0.062) ' <-- you are not using this

Public Function SalesTax(ByVal Quantity As Integer, ByVal item As String) As Double
    Dim TotalWithSalesTax As Double
    If item = "Wheat Bread" Then
        TotalWithSalesTax += (Quantity * 1.15)
    ElseIf item = "White Bread" Then
        TotalWithSalesTax += (Quantity * 1.05)
    ElseIf item = "Donuts" Then
        TotalWithSalesTax += (Quantity * (0.5 * DONUT_TAX) + (Quantity * 0.5))
    ElseIf item = "Raisins" Then
        TotalWithSalesTax += (Quantity * (0.25 * RAISIN_TAX) + (Quantity * 0.25))
    End If
    Return TotalWithSalesTax
End Function

End Module

现在编写代码时,我遇到的唯一问题是“Raisins”的TotalWithSalesTax计算不正确。例如,如果我从列表框中选择“Raisins”并将其数量1添加到另一个列表框中,则消息框中显示的总数为0.00美元。

我开始认为问题出在以下代码部分:

For i As Integer = 0 To lstPurchased.Items.Count - 1
        Dim lst() As String = lstPurchased.Items(i).ToString.Split({CChar(" ")}, 2)

因为我尝试过进行更改,例如

For i As Integer = 1 ...

这导致甜甜圈和葡萄干在消息框中总共给我0.00美元。所以我想知道是否可能如何设置计数是不允许它通过整个列表,但我不知道如何解决这个问题?

我仍然是Visual Basic的新手,一般编程,这是我第一次使用模块。任何人都可以帮我弄清楚我做错了什么,所以我可以转到程序的其他部分吗?

1 个答案:

答案 0 :(得分:1)

我修改了你的代码,希望这就是你想要的。在代码中添加了评论。注意我已删除所有公共变量。

Public Class Form1

    Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAdd.Click
        If (txtQuantity.Text = "") Then
            MessageBox.Show("Please enter a quantity for the item you selected")
        ElseIf Not Integer.TryParse(txtQuantity.Text, txtQuantity.Text) Then
            MessageBox.Show("The quantity entered is not numeric. Please add a numeric quantity.")
            Exit Sub
        Else
            lstPurchased.Items.Add(txtQuantity.Text & " " & lstSale.Text)
        End If
    End Sub

    Private Sub btnCalculate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCalculate.Click
        Dim int As Integer
        Dim total As Double = 0
        'you have to loop through the list to calculate the total
        ' hope this is what you want
        For i As Integer = 0 To lstPurchased.Items.Count - 1
            ' We split the list item into the quantity and the item name
            Dim lst() As String = lstPurchased.Items(i).ToString.Split({CChar(" ")}, 2)
            'lst(1) contains the item name
            Integer.TryParse(lst(0), int)
            total += TaxesModule.SalesTax(int, lst(1))
        Next
        MessageBox.Show(CStr(FormatCurrency(total)))
    End Sub

    Private Sub btnClear_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnClear.Click
        lstPurchased.Items.Clear()
        txtQuantity.Clear()
    End Sub

End Class

模块

Module TaxesModule
    Private Const DONUT_TAX As Decimal = CDec(0.083)
    Private Const RAISIN_TAX As Decimal = CDec(0.02)
    Private Const SS_TAX As Decimal = CDec(0.062) ' <-- you are not using this

    Public Function SalesTax(ByVal Quantity As Integer, ByVal item As String) As Double
        Dim TotalWithSalesTax As Double
        If item = "Wheat Bread" Then
            TotalWithSalesTax = (Quantity * 1.15)
        ElseIf item = "White Bread" Then
            TotalWithSalesTax = (Quantity * 1.05)
        ElseIf item = "Donuts" Then
            TotalWithSalesTax = (Quantity * (0.5 * DONUT_TAX) + (Quantity * 0.5))
        ElseIf item = "Raisins" Then
            TotalWithSalesTax = (Quantity * (0.25 * RAISIN_TAX) + (Quantity * 0.25))
        End If
        Return TotalWithSalesTax
    End Function

End Module