1

问题:为什么在函数Check()下的holdDate,调试时显示“Nothing”,我是通过引用传递的。我想念什么家伙?

问题描述:

我有一个名为 Wallet 的类,我通过传递三个参数在我的主代码中创建了该类的实例(我的表单上已经存在的对象将在以后填充来自用户的数据,而不是立即填充):

Dim myWallet As New Wallet(DataGridView1, DateTimePicker1, "StatementsLog.dat")

在运行时我得到这个: 在此处输入图像描述

如您所见,假设从表单中引用原始对象的对象是空的?我认为如果我通过引用传递(如下所示),该对象将始终显示数据,这将允许我读取它,如上面的屏幕截图所示:

Public Sub New(ByRef Data As DataGridView, ByRef _Date As DateTimePicker, Optional ByVal StatementsFileName As String = "defaultLog.txt")
  'This constructor takes in references to use in class as private
  holdPath = StatementsFileName
  holdData = Data
  holdDate = _Date
End Sub

这是迄今为止我为 Class Wallet 获得的信息:

Option Strict On
Imports System
Imports System.IO

Public Class Wallet

  Private lcheckNumber As Integer = Nothing
  Private lcheckAmount As Decimal = Nothing
  Private ldepositAmount As Decimal = Nothing
  Private lfee As Decimal = Nothing
  Private lDescription As String = Nothing

  Private holdDate As New DateTimePicker
  Private holdData As New DataGridView
  Private holdPath As String = vbNullString

  'Default Constructor
  Public Sub New()
    holdPath = "defaultLog.txt"
  End Sub

  Public Sub New(ByRef _Data As DataGridView, ByRef _Date As DateTimePicker, Optional ByVal StatementsFileName As String = "defaultLog.txt")
    'This constructor takes in references to use in class as private
    holdPath = StatementsFileName
    holdData = _Data
    holdDate = _Date
  End Sub

  'Function Check - Deduct the amount from account and returns current balance.
  Public Function Check(ByVal CheckNumber As Integer, ByVal CheckAmount As Decimal, ByVal Description As String) As Decimal
    Try
      lcheckNumber = CheckNumber
      lcheckAmount = CheckAmount
      lDescription = Description
      lfee = 0D

      Dim _file As New FileStream(holdPath, FileMode.Append, FileAccess.Write)
      Using file As New StreamWriter(_file)
         file.WriteLine(holdDate.Value.ToString & "," & lDescription.ToString & "," & lcheckNumber.ToString & "," & lfee.ToString & "," & lcheckAmount.ToString)
      End Using
    Catch e As IOException
      MessageBox.Show(e.ToString)
    End Try

    Return 0D
  End Function

Form1 代码

Option Strict On
Imports WalletProgram.Wallet

Public Class Form1
    Dim myWallet As New Wallet(DataGridView1, DateTimePicker1, "StatementsLog.dat")


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        optCheck.Checked = True

        'Just test data for DataGridView1
        DataGridView1.Rows.Add(New String() {"12/21/1986", "Test", "44554", "44.22", "45.12"})
    End Sub

    Private Sub cmdAddTransaction_Click(sender As System.Object, e As System.EventArgs) Handles cmdAddTransaction.Click
        If optCheck.Checked Then
            lblAvailableFunds.Text = FormatCurrency(myWallet.Check(CInt(Trim(txtCheck.Text)), CDec(Trim(txtMoney.Text)), txtDescription.Text))
        End If
    End Sub
End Class
4

1 回答 1

1

问题很可能在这里:

Public Class Form1
  Dim myWallet As New Wallet(DataGridView1, DateTimePicker1, "StatementsLog.dat")

这将编译,但这些对象尚未创建,因为它发生表单的InitializeComponent例程被调用之前。

尝试将声明更改为:

Public Class Form1
  Dim myWallet As Wallet

  Protected Overrides Sub OnLoad(e As System.EventArgs)
    _Wallet = New Wallet(DataGridView1, DateTimePicker1)
    MyBase.OnLoad(e)
  End Sub

End Class

在创建控件后创建 Wallet 类的位置。

于 2012-07-08T18:23:03.837 回答