2012-07-08 87 views
1

问题:为什么它在函数Check()下holdDate,调试时显示“Nothing”,我通过引用传递。我在想什么?问题麻烦通过引用私人DataGridView类

说明:

我有一类名为钱包,我通过已经传递了三个参数(对象我的形式,将与来自数据填充上创建在我的主代码类的一个实例用户在以后的时间,不是马上):

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

在运行时我得到这个: enter image description here

正如你所看到的,假设从表单引用原始对象的对象是空的?我想,如果我通过了引用(如下图所示),对象将始终显示的数据,这将让我读它,如上面的截图:

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 

这里就是我有这么迄今为止类钱包:

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 
+1

这些是想到的可能性:1)您正在使用钱包的不同实例。 2)在构造函数和Check之间的某个地方,holdDate被设置为Nothing。 3)你有DateTimePicker的表格已被处理,然后你打电话给wallet.Check() – Steve 2012-07-08 14:03:37

+0

我经历了这些可能性,他们似乎不是问题。 – Dayan 2012-07-08 15:30:23

+0

我知道这个评论没有回答你的问题,但是你真的需要在钱包类里面有一个DateTimePicker吗?你能简单地从外部传递DateTimePicker的值吗? – Steve 2012-07-08 15:55:26

回答

1

的概率LEM是最有可能在这里:

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 

其中已创建的控件后的电子钱包类被创建。

+0

谢谢LarsTech !!! :) – Dayan 2012-07-08 18:58:09