I have written the code and I am new to working with classes. If someone could point me in the right direction I think I can figure it out. I looked at MSDN and learned this much, so maybe I didn't get the right understanding but I thought I would give it a try. When I run the application I use a combobox to select option 1 or option 2 , however this results in an error.
Here is the code:
Imports System.IO
Class Transaction
Private m_prevBal As Double
Private m_date As Date 'Today's date = CStr(Today)
Private m_creditDebit As String 'Deposit or Withdrawl
Private m_amount As Double 'Amount for the transaction
Private m_checkOrSave As String 'Checking or Savings
Public Property TransactionDate() As Date 'Date of transaction being made
Get
Return m_date
End Get
Set(ByVal value As Date)
m_date = value
End Set
End Property
Public Property DepoOrWith() As String 'Deposit or Withdrawl (for both checking and savings)-->DESCRIPTION of transaction
Get
Return m_creditDebit
End Get
Set(ByVal value As String)
m_creditDebit = value
End Set
End Property
Public Property TransactionAmount() As Double 'Amount on transaction attempting to complete
Get
Return m_amount
End Get
Set(ByVal value As Double)
m_amount = value
End Set
End Property
Public Property AcctName() As String 'Checking or Savings account
Get
Return m_checkOrSave
End Get
Set(ByVal value As String)
m_checkOrSave = value
End Set
End Property
Public Property PreviousBalance() As Double
Get
Return m_prevBal
End Get
Set(ByVal value As Double)
m_prevBal = value
End Set
End Property
Public ReadOnly Property NewBalance() As Double
Get
If m_creditDebit = "Deposit" Then 'Need to somehow inclued the transfer from and check clearings.
NewBalance = m_prevBal + m_amount
Return NewBalance
ElseIf m_creditDebit.ToLower = "Withdrawl" Then
NewBalance = m_prevBal - m_amount
Return NewBalance
End If
End Get
End Property
End Class
Class Account
Private m_Balance As Double 'Balance for either checking or savings
Private m_Savings As Account
Private m_transSet() As Transaction
Private m_Checking As Account
Dim fileInput() As String
Public Event negativeBalance(ByVal m_balance As Double)
Public Event approvedTransaction()
Public Property SavAccount() As Account
Get
Return m_Savings
End Get
Set(ByVal value As Account)
m_Savings = value
End Set
End Property
Public Property CheckAccount() As Account
Get
Return m_Checking
End Get
Set(ByVal value As Account)
m_Checking = value
End Set
End Property
Public ReadOnly Property Balance() As Double
Get
Return m_transSet(m_transSet.Length - 1).NewBalance
End Get
End Property
Public WriteOnly Property transDate() As Date
Set(ByVal value As Date)
m_transSet(0).TransactionDate = value
End Set
End Property
Public WriteOnly Property Description() As String
Set(ByVal value As String)
m_transSet(1).DepoOrWith = value
End Set
End Property
Public WriteOnly Property Amount() As Double
Set(ByVal value As Double)
m_transSet(2).TransactionAmount = value
End Set
End Property
Public WriteOnly Property AccountName() As String
Set(ByVal value As String)
m_transSet(3).AcctName = value
End Set
End Property
Sub ReadFile(ByVal NameofAccount As String, ByRef listDisplay As ListBox)
Dim fmtStr As String = "{0,-10} {1,-30}{2,20:c} {3, 10}"
Dim tran As Transaction
Dim x As Integer = 0
Dim sr As StreamReader = Nothing
If NameofAccount = "Savings" Then
sr = File.OpenText("csvSAVINGS.TXT")
Else 'If NameofAccount = "Checking" Then
sr = File.OpenText("csvCHECKING.TXT")
End If
Do While (sr.Peek <> -1)
tran = New Transaction
fileInput = sr.ReadLine.Split(","c)
tran.TransactionDate = fileInput(0)
tran.DepoOrWith = fileInput(1)
tran.TransactionAmount = fileInput(2)
tran.PreviousBalance = fileInput(3)
ReDim Preserve m_transSet(x)
m_transSet(x) = tran
listDisplay.Items.Add(String.Format(fmtStr, tran.TransactionDate.ToShortDateString, tran.DepoOrWith, _
FormatCurrency(tran.TransactionAmount) & " PREV BAL", FormatCurrency(tran.PreviousBalance) & _
" New BAL" & FormatCurrency(tran.NewBalance)))
x += 1
tran = Nothing
Loop
sr.Close()
End Sub
Function CalcBalance(ByVal activity As Transaction) As Double
Dim tran As Transaction
Dim x As Integer = -1
Dim sr As StreamReader = Nothing
If activity.AcctName = "Savings" Then
sr = File.OpenText("csvSAVINGS.TXT")
ElseIf activity.AcctName = "Checking" Then
sr = File.OpenText("csvCHECKING.TXT")
End If
Do While (sr.Peek <> -1)
tran = New Transaction
fileInput = sr.ReadLine.Split(","c)
tran.TransactionDate = fileInput(0)
tran.DepoOrWith = fileInput(1)
tran.TransactionAmount = fileInput(2)
tran.PreviousBalance = fileInput(3)
ReDim Preserve m_transSet(x)
m_transSet(x) = tran
x += 1
tran = Nothing
Loop
sr.Close()
If Balance <= 0 Then
RaiseEvent negativeBalance(Balance)
Else
RaiseEvent approvedTransaction()
End If
Return Balance
End Function
Sub WriteTransaction(ByVal activity As Transaction)
CalcBalance(activity)
Dim temp() As String = {activity.TransactionDate & "," & activity.DepoOrWith.ToString & "," & _
activity.TransactionAmount & "," & Balance}
Select Case activity.AcctName
Case Is = "Savings"
Dim sw As StreamWriter = File.AppendText("csvSAVINGS.TXT")
sw.WriteLine(Join(temp, ","))
sw.Close()
Case Is = "Checking"
Dim sw As StreamWriter = File.AppendText("csvCHECKING.TXT")
sw.WriteLine(Join(temp, ","))
'RaiseEvent approvedTransaction(temp(0))
sw.Close()
Case Is = "Transfer to Checking"
Dim sw1 As StreamWriter = File.AppendText("csvCHECKING.TXT")
Dim sw2 As StreamWriter = File.AppendText("csvSAVINGS.TXT")
Dim temp2() As String = {activity.TransactionDate & "," & activity.AcctName & "," & activity.TransactionAmount}
sw1.WriteLine(Join(temp, ","))
sw2.WriteLine(Join(temp2, ","))
sw1.Close()
sw2.Close()
Case Is = "Transfer to Savings"
Dim sw1 As StreamWriter = File.AppendText("csvSAVINGS.TXT")
Dim sw2 As StreamWriter = File.AppendText("csvCHECKING.TXT")
Dim temp2() As String = {activity.TransactionDate & "," & activity.AcctName & "," & activity.TransactionAmount}
sw1.WriteLine(Join(temp, ","))
sw2.WriteLine(Join(temp2, ","))
sw1.Close()
sw2.Close()
End Select
End Sub
End Class
Public Class form1
Dim calc As Account
Dim activity As Transaction
Private Sub form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'txtDate.Text = CStr(Today) 'Displays today's date
' lblTo.Visible = False
End Sub
Private Sub btnDeposit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeposit.Click
'Sends all deposit data to WriteTransaction, which writes the user data to either csvSAVINGS.TXT or csvCHECKING.TXT
activity = New Transaction 'instance of Transaction
calc = New Account 'instance of Account
activity.TransactionDate = CStr(Today) 'Date of transaction
activity.AcctName = cboAccounts.Text '=Savings or Checking Account chose on form
activity.TransactionAmount = CDbl(txtDeposit.Text) 'Amount depositing into Account selected above
activity.DepoOrWith = grpDeposit.Text 'Description for writing to file
calc.CalcBalance(activity)
calc.WriteTransaction(activity) 'WriteTransaction Method in Class Account, writes all user data to selected file.
txtBalance.Text = FormatCurrency(calc.Balance)
End Sub
Private Sub insufficientFunds(ByVal m_balance As Double)
End Sub
Private Sub ApprovedFunds()
MessageBox.Show("Transaction Complete", "Approved")
End Sub
Private Sub cboAccounts_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboAccounts.SelectedIndexChanged
lstTransactions.Items.Clear()
activity = New Transaction
Dim nameOfAccount As String = cboAccounts.Text
Dim listDisplay As ListBox
If cboAccounts.Text = "Savings" Then
grpCheck.Visible = False
activity.AcctName = "Savings"
Else
grpCheck.Visible = True
activity.AcctName = "Checking"
End If
listDisplay = lstTransactions
calc = New Account
AddHandler calc.approvedTransaction, AddressOf ApprovedFunds
AddHandler calc.negativeBalance, AddressOf insufficientFunds
calc.ReadFile(nameOfAccount, listDisplay)
txtBalance.Text = FormatCurrency(calc.Balance)
End Sub
End Class 'end Form1
This is the line of code which yields the error when selecting the option 1 or 2 in cboAccount.
Public ReadOnly Property Balance() As Double
Get
Return m_transSet(m_transSet.Length - 1).NewBalance
End Get
End Property
It says null reference the value is nothing when in debugger is running
Please any insight would be helpful
If you can help, please do but remember I never took a course in VB so I am a noobie.