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



首先,我对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-then语句中进行(取决于我选择的单选按钮),我希望将它们全部存储起来,这样我就可以编写另一行代码来比较所有这些。。。

我真的很感激你的帮助!

**编辑

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-then语句中使用结果,并在外部使用它们进行计算。我想要标签中一个包装的结果,上面写着每月收费,但我想使用所有三个变量在另一个标签中进行比较,标签上写着潜在的节约

**编辑

为了让你更好地了解我想要什么,我在的末尾写了这篇文章

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

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

您的问题实际上是使用的变量。实际上,您使用的是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期间对数据进行操作。

最新更新