Avatar of BlakeMcKenna
BlakeMcKenna
Flag for United States of America asked on

Creating a Class as an Array?

I have a Class in which I need to either declare it as an array or declare an array within the class and I'm not sure how to do it let alone initialize it. I have attached the class below.

    Public Class clsTEST_DETAILS_Creep
        Private _creep_ID As Integer
        Private _runNO As Short
        Private _intervalPoint As Short
        Private _output As Decimal
        Private _rawOutput As Decimal
        Private _errorPct As Decimal
        Private _timeStamp As DateTime

        Property creep_ID() As Integer
            Get
                Return _creep_ID
            End Get
            Set(value As Integer)
                _creep_ID = value
            End Set
        End Property

        Property runNO() As Short
            Get
                Return _runNO
            End Get
            Set(value As Short)
                _runNO = value
            End Set
        End Property

        Property intervalPoint() As Short
            Get
                Return _intervalPoint
            End Get
            Set(value As Short)
                _intervalPoint = value
            End Set
        End Property

        Property output() As Decimal
            Get
                Return _output
            End Get
            Set(value As Decimal)
                _output = value
            End Set
        End Property

        Property rawOutput() As Decimal
            Get
                Return _rawOutput
            End Get
            Set(value As Decimal)
                _rawOutput = value
            End Set
        End Property

        Property errorPct() As Decimal
            Get
                Return _errorPct
            End Get
            Set(value As Decimal)
                _errorPct = value
            End Set
        End Property

        Property timeStamp() As DateTime
            Get
                Return _timeStamp
            End Get
            Set(value As DateTime)
                _timeStamp = value
            End Set
        End Property
    End Class

Open in new window

Visual Basic.NET

Avatar of undefined
Last Comment
BlakeMcKenna

8/22/2022 - Mon
5teveo

try

Dim x(10) As clsTEST_DETAILS_Creep
BlakeMcKenna

ASKER
Ok, I tried that already and got the infamous "Object not set to an instance of an object" error. I'm declaring this array in a Form like this:

    Private arrCreepD(0) As CalTableClasses.CalibrationClasses.clsTEST_DETAILS_Creep

Open in new window


But I get that error the very first time I try to assign a value to the the "creep_ID". Here is the logic where I try that.

            For Each row As DataGridViewRow In dgvCreep.Rows
                ReDim Preserve arrCreepD(x)

                arrCreepD(x).creep_ID = iCreep_ID
                arrCreepD(x).runNO = cmbCreepRunNO.Text
                arrCreepD(x).intervalPoint = row.Cells(1).Value
                arrCreepD(x).output = row.Cells(2).Value
                arrCreepD(x).rawOutput = row.Cells(5).Value
                arrCreepD(x).errorPct = row.Cells(4).Value
                arrCreepD(x).timeStamp = row.Cells(6).Value
            Next

Open in new window

ASKER CERTIFIED SOLUTION
5teveo

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
BlakeMcKenna

ASKER
  For I As Integer = 3 To 9
            x(I) = New clsTEST_DETAILS_Creep
            x(I).creep_ID = I
  Next

Open in new window


The above code worked!

Thanks!
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Nasir Razzaq

I suggest you use a List which makes more sense here since the size is unknown. If you still need an array at the end, you can call the ToArray method of the list.


Dim MyList As New List(Of clsTEST_DETAILS_Creep)
            For Each row As DataGridViewRow In dgvCreep.Rows
                Dim arrCreepD As New clsTEST_DETAILS_Creep

                arrCreepD.creep_ID = iCreep_ID
                arrCreepD.runNO = cmbCreepRunNO.Text
                arrCreepD.intervalPoint = row.Cells(1).Value
                arrCreepD.output = row.Cells(2).Value
                arrCreepD.rawOutput = row.Cells(5).Value
                arrCreepD.errorPct = row.Cells(4).Value
                arrCreepD.timeStamp = row.Cells(6).Value
     
               MyList.Add(arrCreepD)
            Next

Open in new window

BlakeMcKenna

ASKER
This code worked!

 For I As Integer = 3 To 9
            x(I) = New clsTEST_DETAILS_Creep
            x(I).creep_ID = I
        Next
BlakeMcKenna

ASKER
How can I keep track of how many groups of data there are?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.