How to do Array of Classes?

I have a property-only class defined as DayClass.

I create a property of WindowX, MyDayClass() as DayClass.

I try to do in the window’s open event:
MyDayClass(-1) as new DayClass

Then, when I know what the dimension needs to be:
Redim WindowX.MyDayClass(DayCount -1)

The compiler seems happy with it all BUT …
An Out of Bounds exception is generated with the new statement.

I also try

WindowX.MyDayClass(DayCount-1) as new DayClass

Same exception.

If I simply do (without the new)
Redim WindowX.MyDayClass(DayCount - 1)

It is executed without complaint BUT a later reference to WindowX.MyDayClass(index).AProperty

generates a Nil Object exception. When I inspect the contents of WindowX.MyDayClass I see six “rows” (the dimension is 6 in the example that was run) and the rows are all nil.

So, what is the correct incantation for creating an array of classes? My earlier effort used an array of structs but string handling, was, shall we say, painful! But, I COULD create a run-time dimensioned array of structs as I tried with a class in the first try, above.

As a painful fall-back, I COULD create an independent array for each property of DayCass, but it seems much more sensible to handle them ensemble.

Many thanks

Jim Wagner
Oregon Research Electronics

Property MyDayClass() As DayClass

// In code...
MyDayClass.Add new DayClass
// or...
MyDayClass.ResizeTo( DayCount - 1 )
MyDayClass( 0 ) = new DayClass // Assumes DayCount > 0

You cannot access element -1 of an array, ever. Hence your OutOfBoundsException.

When you create an array of a class, you create slots for that class, but each slot starts off as Nil. It’s up to you to assign something to that slot.

So, even if the original DayClass has defined properties, those classes are unknown to the array? If that is the case, then there is no reason for such an array, in spite of the UG saying that you really should use a class instead of a struct?

Oh, create the array, then have to assign a new DayClass to each element of the “array”?

Thanks
Jim

I’m not sure what you’re asking.

Need to try your suggestion now that I think that I understand it a bit better. May be back. Thanks for your help!

That works!

Jim

1 Like