Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble passing reference to private DataGridView of Class
    primarykey
    data
    text
    <p><strong>Question: Why is it that holdDate under function Check(), show "Nothing" while debugging, I was passing by reference. What am i missing guys??</strong> </p> <p><strong>Description of problem:</strong></p> <p>I have a class named Wallet, I created instance of that class in my main code by passing three parameters (Objects already on my form that will be populated with data from the user at a later time, not right away):</p> <pre><code>Dim myWallet As New Wallet(DataGridView1, DateTimePicker1, "StatementsLog.dat") </code></pre> <p><strong>At run time I get this:</strong> <img src="https://i.stack.imgur.com/3Yc2f.jpg" alt="enter image description here"></p> <p>As you can see, the object that was suppose to reference the original object from the form is empty? I thought that if i passed by reference (as shown below) that the object will always show the data, and that would allow me to read it as shown in the screenshot above:</p> <pre><code>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 </code></pre> <p><strong>Here's what i got so far for Class Wallet:</strong></p> <pre><code>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 &amp; "," &amp; lDescription.ToString &amp; "," &amp; lcheckNumber.ToString &amp; "," &amp; lfee.ToString &amp; "," &amp; lcheckAmount.ToString) End Using Catch e As IOException MessageBox.Show(e.ToString) End Try Return 0D End Function </code></pre> <p><strong>Form1 Code</strong></p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload