从子过程vb.net获得0值

时间:2012-09-13 16:28:54

标签: vb.net

您已经分配了一个家庭作业来创建以下程序:手机公司,Cell4U,确定他们的手机费用如下:客户支付R2.05全分钟通话和2c额外每秒。他们还支付每个短信40c,但客户每完整30个通话时间可获得2个免费短信。每发送30条短信,客户也可免费获得1条短信。

为公司工作的所有员工都可获得免费手机作为额外费用,他们的帐户每月最高支付R800。该公司要求每月向所有员工报告手机费用以及他们为员工支付手机费用的总金额。

该计划必须提供以下内容:

•必须在子程序中计算呼叫的到期金额和sms消息的到期金额。这必然意味着子过程必须将秒转换为分钟和秒,并且这些值以及自由短信的数量也可用于主过程。 •必须调用第二个子程序来计算员工的总账单,并累计公司为手机津贴支付的总额。此子过程还必须使用布尔参数来向主程序指示公司是否将支付该员工的全部金额或仅支付R800。 •所有输出必须显示在主程序中(按钮单击事件)。 •任意数量的员工。空员工编号将指示输入的结束(或者用户可以单击输入框中的取消按钮)。当输入最后一个数据时,必须显示总金额,如图5所示。请记住,如果一个帐户超过R800,公司只向该帐单提供R800,并且只有R800必须反映在总数中。

我已经编写了以下内容但代码但是返回零值

Option Strict On
Option Explicit On

Public Class TheCellPhoneCompany

Private Sub CalcMinAndSec(ByVal intLengthCallInSec As Integer, ByVal intCallSec As Integer, ByVal intCallMin As Integer, ByVal intNumMsgs As Integer, ByVal intNumFreeMsgs As Integer, ByVal decCallCost As Decimal, _
                          ByVal decSmsCost As Decimal, ByVal RatePerMin As Decimal, ByVal RatePerSec As Decimal, ByVal RatePerSms As Decimal)

    intCallMin = (intLengthCallInSec \ 60I)
    intCallSec = ((intLengthCallInSec Mod 60I))
    intNumFreeMsgs = ((intCallMin - (intCallMin Mod 30)) \ 15) + ((intNumMsgs - (intNumMsgs Mod 30I) \ 30I))
    decCallCost = (intCallMin * RatePerMin) + (intCallSec * RatePerSec)
    decSmsCost = (intNumMsgs - intNumFreeMsgs) * RatePerSms

End Sub

Private Sub CalcTotals(ByVal decCallCost As Decimal, ByVal decSmsCost As Decimal, ByVal decTotCost As Decimal, ByVal decAmtPaid As Decimal, ByVal decFinalAmtPaid As Decimal, ByVal PaidByComp As Decimal)

    Dim blnPaidByComp As Boolean = decTotCost > 800I

    decTotCost = decCallCost + decSmsCost
    If blnPaidByComp = True Then
        decAmtPaid = PaidByComp
    Else
        decAmtPaid = decTotCost
    End If

    decFinalAmtPaid += decAmtPaid

End Sub

Private Sub btnEnterData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnterData.Click

    Dim strEmpCode, strLengthCallInSec, strNumOfSms As String
    Dim intNumMsgs, intNumFreeMsgs, intCallMin, intCallSec, intEmpCode, intLengthCallInSec As Integer
    Dim decCallCost, decSmsCost, decTotCost, decAmtPaid, decFinalAmtPaid As Decimal
    Dim intEmpNum As Integer = 1

    Const RatePerMin As Decimal = 2.05D
    Const RatePerSec As Decimal = 0.02D
    Const RatePerSms As Decimal = 0.4D
    Const PaidByComp As Decimal = 800D

    Do

        strEmpCode = InputBox("Please enter employee number", "Employee " & intEmpNum)
        Integer.TryParse(strEmpCode, intEmpCode)
        If intEmpCode <> 0 Then


            strLengthCallInSec = InputBox("Enter total calls for the month in seconds", "Employee " & intEmpNum)
            Integer.TryParse(strLengthCallInSec, intLengthCallInSec)

            If intLengthCallInSec > 0 Then
                strNumOfSms = InputBox("Enter total number of SMS's sent for the month", "Employee " & intEmpNum)
                Integer.TryParse(strNumOfSms, intNumMsgs)

                If intNumMsgs > 0 Then

                Else
                    MessageBox.Show("Invalid value - the number of SMS's must be a positive integer", "Error - please correct", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
                End If

            Else
                MessageBox.Show("Invalid value - the seconds must be a positive integer", "Error - please correct", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
            End If

        Else
        End If

        Call CalcMinAndSec(intLengthCallInSec, intCallSec, intCallMin, intNumMsgs, intNumFreeMsgs, decCallCost, decSmsCost, RatePerMin, RatePerSec, RatePerSms)

        Call CalcTotals(decCallCost, decSmsCost, decTotCost, decAmtPaid, decFinalAmtPaid, PaidByComp)

        lstCellPhones.Items.Add("Employee: " & intEmpCode)
        lstCellPhones.Items.Add("Calls: " & intCallMin & " minutes and " & intCallSec & " seconds")
        lstCellPhones.Items.Add("Number of SMS messages: " & intNumMsgs & " (" & intNumFreeMsgs & ")")
        lstCellPhones.Items.Add("Cost for calls: R" & decCallCost.ToString("N2"))
        lstCellPhones.Items.Add("Cost for sms messages: R" & decSmsCost.ToString("N2"))
        lstCellPhones.Items.Add("Total cost: R" & decTotCost.ToString("N2"))
        lstCellPhones.Items.Add("Paid by company: R" & decAmtPaid.ToString("N2"))
        lstCellPhones.Items.Add("")

        intEmpNum = intEmpNum + 1


    Loop Until intEmpCode = 0

    lstCellPhones.Items.Add("Total amount paid by company for cell phones = R" & decFinalAmtPaid.ToString("N2"))




End Sub

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

    lstCellPhones.Items.Clear()

End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click

    Me.Close()

End Sub
End Class

1 个答案:

答案 0 :(得分:4)

1)通常将值传递给sub以获取更改的值。如果您改变了decCallCost等变量的范围,以便它们对您的所有代码都“可见”,那么您将可以通过某种方式使其工作。

2)在线

decSmsCost = (intNumMsgs - intNumFreeMsgs) * RatePerSms

如果intNumFreeMsgs&gt;会发生什么? intNumMsgs?

3)虽然你通常不会这样做,因为这是一个错误的想法,你可以将作为参数传递的变量更改为sub:

Module Module1

    Sub x(ByRef n As Integer)
        n = 5
    End Sub

    Sub Main()
        Dim a As Integer = 1
        x(a)
        Console.WriteLine(a) ' outputs 5
        Console.ReadLine()
    End Sub

End Module

以防你在某处看到它并想知道发生了什么。