/ / Problem z przekazaniem odwołania do prywatnego DataGridView klasy - vb.net, winforms

Problem z przekazaniem odwołania do prywatnego DataGridView of Class - vb.net, winforms

Pytanie: Dlaczego holdDate w funkcji Check () pokazuje „Nic” podczas debugowania, przechodziłem przez referencję. Czego brakuje mi chłopaki?

Opis problemu:

Mam klasę o nazwie Portfel, utworzyłem instancjętej klasy w moim głównym kodzie, przekazując trzy parametry (obiekty już w moim formularzu, które zostaną wypełnione danymi użytkownika w późniejszym czasie, nie od razu):

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

W czasie wykonywania otrzymuję to: wprowadź opis obrazu tutaj

Jak widać obiekt, który miałodwołanie oryginalny obiekt z formularza jest pusty? Pomyślałem, że jeśli przejdę przez odniesienie (jak pokazano poniżej), obiekt zawsze pokaże dane, a to pozwoli mi je odczytać, jak pokazano na zrzucie ekranu powyżej:

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

Oto, co do tej pory dostałem dla Portfela klasowego:

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

Kod 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

Odpowiedzi:

1 dla odpowiedzi № 1

Problem jest najprawdopodobniej tutaj:

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

To się skompiluje, ale te obiekty nie zostały jeszcze utworzone, odkąd to się stało przed formularz „s InitializeComponent rutyna zostaje wywołana.

Spróbuj zmienić deklarację na następującą:

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

gdzie klasa Wallet zostaje utworzona po utworzeniu formantów.