将结果存储在if / then语句之外

时间:2014-09-16 00:53:31

标签: vb.net

拳头我对vb真的很新,所以我不确定这个问题是否有意义,但我会尽力解释它

所以我正在编写一个程序来计算互联网服务提供商的不同套餐的成本(我正在学习的书中的挑战之一)。该程序有3个单选按钮(用于选择包)和一个文本框来输入小时数。根据您选择的包裹和您输入的小时数,它会提供价格的输出。

到目前为止它是这样的:

Dim decMonthlyCharge As Decimal
Dim intHours As Integer
Dim decPackageA As Decimal
Dim decPackageB As Decimal
Dim decPackageC As Decimal

Try
    intHours = CInt(txtNumberofHours.Text)

    If radPackageA.Checked = True Then
        If intHours <= 10 Then
            decPackageA = CDec(9.95)
        ElseIf intHours >= 11 Then
            decPackageA = CDec(intHours - 10) * 2 + CDec(9.95)
        End If

        If chkNonProfit.Checked = True Then
            decPackageA = CDec(decPackageA - (decPackageA * 0.2))
        End If

        decMonthlyCharge = decPackageA
    End If

对于使用不同变量检查的每个单选按钮,重做此项。

好的,所以我声明包A,B,C的原因是我希望程序存储每个选项的值,然后添加另一个复选框,显示与所选计划相比的节省。 我遇到的麻烦是这些计算只在if语句中完成(取决于我选择的单选按钮)并且我希望它们全部存储,以便我可以编写另一行代码来比较所有这些...

我真的很感激一些帮助!

**修改

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

    Dim decMonthlyCharge As Decimal
    Dim intHours As Integer
    Private decPackageA As Decimal
    Private decPackageB As Decimal
    Private decPackageC As Decimal



    Try
        intHours = CInt(txtNumberofHours.Text)
        If radPackageA.Checked = True Then
            If intHours <= 10 Then
                decPackageA = CDec(9.95)
            ElseIf intHours >= 11 Then
                decPackageA = CDec(intHours - 10) * 2 + CDec(9.95)
            End If
            If chkNonProfit.Checked = True Then
                decPackageA = CDec(decPackageA - (decPackageA * 0.2))
            End If
            decMonthlyCharge = decPackageA
        End If

我像他说的那样宣称他们是私人的,但我认为我做得不对。我所说的是如何在if语句中使用结果并将它们用于外部计算。我希望标签中的一个包的结果显示每月费用,但我想使用所有三个变量来比较另一个标明可能节省的标签

**编辑

为了让你更好地了解我想要的东西,我在最后写了这个

lblMonthlyCharge.Text = decMonthlyCharge.ToString("c")

        If chkPotentialSavings.Checked = True Then

            lblPotentialSavings.Text = CStr(decPackageA - decPackageB) & " potential savings with package B"
            End If

**好的,上次修改

我在方法之外声明了变量,并编写了以下内容

'使用套餐A计算节省

        If chkPotentialSavings.Checked = True And decMonthlyCharge = decPackageA Then
            lblPotentialSavings.Text = (decPackageA - decPackageB).ToString("C") & " potential savings with package B,  " &
                (decPackageA - decpackagec).ToString("c") & " potential savings with package C"

            'calculate savings with Package B
        ElseIf chkPotentialSavings.Checked = True And decMonthlyCharge = decPackageB Then
            lblPotentialSavings.Text = (decPackageB - decPackageA).ToString("C") & " potential savings with package A,  " &
                (decPackageB - decpackagec).ToString("c") & " potential savings with package C"

            If (decPackageB - decPackageA) <= (0) Then
                lblPotentialSavings.Text = "$0.00 potential savings with package A, " &
                    (decPackageB - decpackagec).ToString("c") & " potential savings with package C"

            End If
            'Calculate savings with Package C
        ElseIf chkPotentialSavings.Checked = True And decMonthlyCharge = decpackagec Then
            lblPotentialSavings.Text = (decpackagec - decPackageA).ToString("C") & " potential savings with package A,  " &
                (decpackagec - decPackageB).ToString("c") & " potential savings with package C "
            If (decpackagec - decPackageA) <= (0) Then
                lblPotentialSavings.Text = "$0.00 potential savings with package A,  " &
                    (decpackagec - decPackageB).ToString("c") & " potential savings with package B "
                If (decpackagec - decPackageB) <= (0) Then
                    lblPotentialSavings.Text = " $0.00 potential savings with package A,  " & " $0.00 potential savings with package B"
                End If

            End If

问题是它只在我点击每个单独的单选按钮后存储销售额,然后单击每个按钮的计算按钮。我如何解决这个问题,以便它自动进行计算而不必单击每个单独的计算按钮?

1 个答案:

答案 0 :(得分:0)

您的问题实际上是使用的变量。实际上你使用的是Local变量,它们在方法中声明,只能在方法中使用。

要使变量在整个类中可用,也就是说,在不同的方法中,您需要一个Global变量,该变量声明为Private decPackageA as decimal。请注意,声明应在方法之外完成(在课程开始时,为了更好的可见性)。

或者您可以将其声明为全局Property,可在整个课程中访问,如下所示;

    Private _decPackageA As Decimal
    Public Property decPackageA() As Decimal
        Get
            Return _decPackageA
        End Get
        Set(ByVal value As Decimal)
            _decPackageA = value
        End Set
    End Property

该属性可作为全局变量访问。其主要目的是可以在SETGET期间操纵数据。