Storing class in an array?

I have almost got my head around the concept of storing a class in an array but it is still not working as expected and I cant see what I am doing wrong.

I have a class called “clsProductList” and properties called “ID, Name, Price”. I scan through a database getting back records and doing the following through each cycle of the recordset.

Dim prod as New clsProductList prod.Name = data.Field("ProductName").StringValue prod.ID= data.Field("ProductID").Integer prod.Price = data.Field("ProductPrice").DoubleValue aryPrescriptionPad.Append (prod)

My understanding is that each product is now stored in the array row so on another page I do the following:

for lp as integer = 0 to 10 lblProductName(lp).Text = aryPrescriptionPad(lp).ProductName next

I know the code above only works because it only works with 11 products but is just for example as I have 11 lblProductName labels again for testing.

The problem is that the aryPrescriptionPad(lp).ProductName is always giving me a Nil Object error even on the 0 iteration. I have checked my code where the array is populated and it has data in it.

I know I am probably doing something really stupid but it is totally got me confused as it almost works and I can now see the power of using classes within arrays.

It sounds like you aren’t Dimming aryPrescriptionPad in the correct place. Try making it a window property instead of defining it in a method and see if that helps.

In the For loop, try something like this:

Dim p As clsProductList = aryPrescriptionPad(lp) lblProductName(lp).Text = p.Name

I am stabbing in the dark but I assume you are dimming aryPrescriptionPad() as clsProductList. I am not sure if append will work with a class as you have not initialised the array.

Try:

Redim aryPrescriptionPad(ubound(aryPrescriptionPad + 1))
aryPrescriptionPad.ubound = new clsProductList

Dim prod as New clsProductList
prod.Name = data.Field("ProductName").StringValue
prod.ID= data.Field("ProductID").Integer
prod.Price = data.Field("ProductPrice").DoubleValue
aryPrescriptionPad(ubound) = prod

[quote=86422:@Nathan Wright]Dim prod as New clsProductList
prod.Name = data.Field(“ProductName”).StringValue
prod.ID= data.Field(“ProductID”).Integer
prod.Price = data.Field(“ProductPrice”).DoubleValue
aryPrescriptionPad.Append (prod)[/quote]

My bet is that your dimming the array to be the right size. ie
dim aryPrescriptionPad(10)
when you use
aryPrescriptionPad.Append
you’re adding new values to the end of the array (hence the nilobject rather than outofbounds)

dim aryPrescriptionPad()
will create an empty array for appending.

I would first opt for Bill’s solution. Where do you dim aryPrescriptionPad? Seems that it doesn’t exist after appending it – did you dim it in the method above? Then it will only live while the method is running.

Redimming shouldn’t be necessary if you define it as a global (app-based or window-based) property in the UI:

aryPrescriptionPad as prod().

If you define it in the window, it will of course vanish once you close the window.

Sorry, brackets on wrong place. Look at Jim‘s posting. (Why can’t I edit my former post now?)

I tried Thomas and Mikes solutions but that gave the same error.

I have created the property in a module as “aryPrescriptionPad()”, should this have a -1 in the brackets rather than nothing?

Ulrich where do you suggest I put the sample code you gave, in the part that populates the array or where I am trying to reference it?

If you have dim aryPrescriptionPad as prod(10) the APPEND makes item #11… so you never have an item zero

As mentioned above… use dim aryPrescriptionPad as prod(-1)

I prefer -1 to blank

Ok I have found the problem but not sure why. Looking in the debugger it would appear that the 0 row of the array is nil but all the following rows are correct. Is this because I have the array declared with blank rather than -1

No I have just tried -1 and getting the same thing. So if you have an empty array and then use append is it expected that the array becomes 1 based rather than 0 based?

Ok, now got it to work, I had to Redim the aryPrescriptionPad using -1 before starting the append process.

Thanks everyone for you help.

Blank parens and -1 are the same thing. Dave prefers the latter, I prefer the former, but it’s nothing more than style in any case.

Nathan: No, it’s not meant as sample code. I would declare the array in the Editor, meaning put a property arePrescriptionPad in preferably the App or a Module. This way you make sure it is global and not unintentionally deleted when you close the window or leave the method it is created (at least if there is the need to keep it persistent, that is).

I don’t think you’ll need to put a -1 into the declare. Empty brackets should do the job.

Do you unintentionally perform some initialization action on the array so it contains one element (even if that is NIL) on position 0?

(Too late I see – happy to read it works now. If you do not declare the array with 10 elements initially but leave the brackets empty, you’ll need no redim.)

[quote=86442:@Nathan Wright]I have created the property in a module as “aryPrescriptionPad()”, should this have a -1 in the brackets rather than nothing?
[/quote]

Either way gives the same result… it sounds like maybe you have a “redim” statement lurking somewhere?
When the nilObjectException occurs, what is the size of the array?

As general rule: If you dim an array with some bounds set (like in your example aryPrescriptionPad (10)), it is limited to the number of elements. Append will not work, instead you’ll get an outofbounds error.

If you leave the brackets empty (or put a -1 in them), the array is dynamic. You can, as you’ve seen, convert it between both states by redimming it. But then you’ll lose the former contents (not if you redim it from 10 to 11 or so of course).

you could, not you will … damn you, fast mouse click-finger!

[quote=86457:@Ulrich Bogun]As general rule: If you dim an array with some bounds set (like in your example aryPrescriptionPad (10)), it is limited to the number of elements. Append will not work, instead you’ll get an outofbounds error.

If you leave the brackets empty (or put a -1 in them), the array is dynamic. You can, as you’ve seen, convert it between both states by redimming it. But then you’ll lose the former contents (not if you redim it from 10 to 11 or so of course).[/quote]

This is completely incorrect
Arrays are always dynamically sized

A couple simple tests will show you this
Try

dim i(6) as integer i.append 8 break
or

dim i(6) as date i.append new date break

You can/could get an out of bounds error in the case where you expect the array has 10 elements but in reality it has fewer
This is where checking ubound is a good thing

No its not 1 based it is still 0 based with nil in the 0 bound. As others have said Dim YourArray() prior to using append and then your first element will be in YourArray(0)

Thanks for your clarification, Norman, I got that wrong.