Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27379

[RESOLVED] Create a class and Use it as an array

$
0
0
Hello everyone,

I am back with another question and of course it had to be arrays (This is a nightmare for me) I wonder how important it is to learn arrays 100% because this is giving me a hard time lol.

So I am working on an application that gets information from a customer: Name,Check number, Date and amount and shows it in a listbox, I have to use a user defined class to accomplish this.

This is my class:

Code:

Public Class Transaction

    Public Property CheckNo As String
    Public Property Payable As String
    Public Property CheckDate As String
    Public Property Amount As String


    Public Sub New()
        CheckNo = String.Empty
        Payable = String.Empty
        CheckDate = String.Empty
        Amount = String.Empty
    End Sub

    Public Function GetLine() As String
        Return CheckNo & CheckDate & Payable & Amount
    End Function
End Class

And this is the code I have to far, but its returning a null reference exception. I know it has to do with the declaration of the array but I cannot figure it out.

Code:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim MyTransactions() As Transaction

        ' Save new items in the array
        ' See if the value is null first
        If MyTransactions.Length = Nothing Then
            ReDim MyTransactions(0)
        Else
            ReDim Preserve MyTransactions(MyTransactions.Length + 1)
        End If
        ' Get the values
        MyTransactions(MyTransactions.Length + 1).CheckNo = txtCheckNumber.Text.PadLeft(8)
        MyTransactions(MyTransactions.Length + 1).CheckDate = dtpCheckDate.Value.Date.ToShortDateString.PadLeft(8)
        MyTransactions(MyTransactions.Length + 1).Payable = txtName.Text.PadLeft(20)
        MyTransactions(MyTransactions.Length + 1).Amount = txtAmount.Text.PadLeft(8)


        ' Add the value to the listbox
        For subIndex As Integer = 0 To MyTransactions.Length - 1
            lstTransactions.Items.Add(MyTransactions(subIndex).GetLine)
        Next

    End Sub

Thanks a lot for the help :)

Viewing all articles
Browse latest Browse all 27379

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>