Link to home
Start Free TrialLog in
Avatar of BlakeMcKenna
BlakeMcKennaFlag 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

Avatar of 5teveo
5teveo
Flag of United States of America image

try

Dim x(10) As clsTEST_DETAILS_Creep
Avatar of 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
Avatar of 5teveo
5teveo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
  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!
Avatar of 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

This code worked!

 For I As Integer = 3 To 9
            x(I) = New clsTEST_DETAILS_Creep
            x(I).creep_ID = I
        Next
How can I keep track of how many groups of data there are?