How to have your class track its instances?

mrdippy

Member
Joined
Mar 27, 2008
Messages
9
Programming Experience
5-10
In other languages I used this pattern to keep tabs on all the instances of my class within the class itself, just take a ref to the instance within the constructor and push it onto a static array.

When I try creating an instance of this bogus lil class I get the "Object reference not set to an instance of the object" on the records.add(Me) line.

I need a clue... Is this just not a fit for VB .NET coding style? I have a couple C# books, its kinda hard to find a nefarious question like this in their index or by flippin through. Thanks!

VB.NET:
  Public Class Record
        Public Shared records As List(Of Record)

        Dim _artist As String
        Dim _album As String

        Public Sub New()
            records.Add(Me)
        End Sub

    End Class
 
Firstly, the reason you are getting the error is that you havent initialised the list (ie New List) - see my posts 7 and 8 when I did the same thing :eek:

However, if I was writing this, I'd do something like :-

VB.NET:
Option Strict On

Public Class cRecordCollection
    Inherits System.Collections.ObjectModel.Collection(Of Record)
End Class

Public Class Record

    Private _artist As String
    Private _album As String

    Public Sub New(ByVal Artist As String, ByVal Album As String)
        _artist = Artist
        _album = Album
    End Sub


    Public Property Artist() As String
        Get
            Return _artist
        End Get
        Set(ByVal value As String)
            _artist = value
        End Set
    End Property

    Public Property Album() As String
        Get
            Return _album
        End Get
        Set(ByVal value As String)
            _album = value
        End Set
    End Property

End Class

and

VB.NET:
        Dim RecordCollection As New cRecordCollection
        RecordCollection.Add(New Record("Enya", "Watermark"))
        RecordCollection.Add(New Record("Queen", "Greatest Hits"))

I'm sure someone will correct me if that's not the best way to do it :D
 
Last edited:
Oh my... I feel like a newbie, well I am. Your approach of using properties is right on with my plans - I kept the example bare bones to the krux of my issue. I just had to add the word "New" and all fixed.

I think a generic List is all I need... and I think the most efficient means of storage too. Time will tell, thanks for your answer and approach. I'm still learning the VB .Net style. I thought there were some special rules I was oblivious to about constructors or the "Me" variable - now i'm embarassed, feel like I forgot a semicolon.

Building on this lil example within my Form1 form/class I have a DataGridView1 and set its "DataSource" to record.records, so the gridview displays all my objects as rows by accessing the List of objects through the shared List. Kinda a lightweight dataset of objects if you will. Whether thats good vb style or not its what I'm used to coding and seems like a good deal. (e.g. DataGridView1.DataSource=Record.records)
 
Last edited:
Back
Top