每次我离开并重新进入班级时,Visual Basic 都会将我的变量重置为 0

问题描述 投票:0回答:3

我有一个表格可以从用户那里获取财务信息,包括贷款金额、利率和期限。我应该打电话给一个班级来完成这项工作,然后将答案返回到表格中。然而,由于某种原因,每当我的程序移出类,返回表单,然后返回类时,我在类中的变量就会重置为 0。我的代码如下。任何帮助都会很棒。

这是我表单中的代码:

    'Preform calculation when button is clicked
Private Sub buttonCalc_Click(sender As Object, e As EventArgs) Handles buttonCalc.Click
    Dim loanAmount As New finClass
    Dim rate As New finClass
    Dim term As New finClass
    Dim payment As New finClass
    loanAmount.getLoanAmount = textBoxMortgageAmount.Text
    rate.getAnnualRate = comboBoxAnnualRate.Text.Replace("%", "")
    term.getTermInYears = comboBoxTerm.SelectedItem
    textBoxMonthlyPayment.Text = payment.setMonthlyPayment

End Sub

来自关联类的代码:

Public Class finClass

Private loanAmt As Decimal
Private termValue As Decimal
Private rateValue As Decimal
Public paymentValue As Decimal

Public WriteOnly Property getLoanAmount
    Set(value)
        If IsNumeric(value) AndAlso value > 1000 AndAlso value < 10000000 Then
            Me.loanAmt = value
        Else
            MessageBox.Show("Mortgage amount must be a number between $1,000 and $10,000,000", "Invalid Number", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Set
End Property

Public WriteOnly Property getAnnualRate
    Set(value)
        rateValue = value.Replace("%", "")
        If rateValue < 1 Then
            rateValue *= 100
        End If

    End Set
End Property

Public WriteOnly Property getTermInYears
    Set(value)
        termValue = value
    End Set
End Property

Public ReadOnly Property setMonthlyPayment
    Get
        Return -Pmt((rateValue / 1200), termValue * 12, Me.loanAmt)
    End Get
End Property
End Class
vb.net visual-studio-2012
3个回答
5
投票

我不在 VS 中工作,但看起来您正在创建该类的四个实例。我很确定这是不正确的。

Private Sub buttonCalc_Click(sender As Object, e As EventArgs) Handles buttonCalc.Click
    Dim loanAmount As New finClass  '## Creates NEW finClass object
    Dim rate As New finClass        '## Creates NEW finClass object
    Dim term As New finClass        '## Creates NEW finClass object
    Dim payment As New finClass     '## Creates NEW finClass object
    loanAmount.getLoanAmount = textBoxMortgageAmount.Text
    rate.getAnnualRate = comboBoxAnnualRate.Text.Replace("%", "")
    term.getTermInYears = comboBoxTerm.SelectedItem
    textBoxMonthlyPayment.Text = payment.setMonthlyPayment

End Sub

因此,每个对象都是 finClass 对象的

不同
实例。所以每次你“重新进入”类时,你实际上进入了该类对象的一个不同实例,这就是为什么所有变量的值= 0。它们还没有被实例化或设置。

尝试这样做,创建一个类对象,您可以使用类模块中的方法来操作它:

Private Sub buttonCalc_Click(sender As Object, e As EventArgs) Handles buttonCalc.Click
    Dim objFinClass as New finClass

    objFinClass.getLoanAmount = textBoxMortgageAmount.Text
    objFinClass.getAnnualRate = comboBoxAnnualRate.Text.Replace("%", "")
    objFinClass.getTermInYears = comboBoxTerm.SelectedItem
    textBoxMonthlyPayment.Text = objFinClass.setMonthlyPayment

End Sub

0
投票

创建一个模块并使用“公共”来衡量变量 公共模块示例 公共示例变量作为整数 结束模块

这将允许变量在程序运行期间保留其值。在表单类中使用 dim 会导致每次加载表单时该值都会重新调整


0
投票

您遇到的另一个问题是,当您的表单完成私有子例程时,它会将您的所有变量集作为 finclass 丢弃。这意味着当您再次按下时,它将重新开始为 0。您将需要在表单类中创建类的变量,这样当您回调它时就不会重置它。

公开课F_Form1 将 objFinClass 调暗为新 finClass Private Sub ButtonCalc_Click(sender As Object, e As EventArgs) 处理buttonCalc.Click

objFinClass.getLoanAmount = textBoxMortgageAmount.Text
objFinClass.getAnnualRate = comboBoxAnnualRate.Text.Replace("%", "")
objFinClass.getTermInYears = comboBoxTerm.SelectedItem
textBoxMonthlyPayment.Text = objFinClass.setMonthlyPayment

结束子 下课

© www.soinside.com 2019 - 2024. All rights reserved.