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.
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 Bills solution. Where do you dim aryPrescriptionPad? Seems that it doesnt exist after appending it did you dim it in the method above? Then it will only live while the method is running.
Redimming shouldnt 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.
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?
Nathan: No, its 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 dont think youll 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, youll 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 youll get an outofbounds error.
If you leave the brackets empty (or put a -1 in them), the array is dynamic. You can, as youve seen, convert it between both states by redimming it. But then youll lose the former contents (not if you redim it from 10 to 11 or so of course).
[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 youll get an outofbounds error.
If you leave the brackets empty (or put a -1 in them), the array is dynamic. You can, as youve seen, convert it between both states by redimming it. But then youll 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)