Problems using an array of user classes

quid007

New Member
Joined
Oct 21, 2010
Messages
1
I am trying to loop through an array of user defined objects (classes) and am experiencing some difficulties. The code skeleton is as follows.

*************************
dim devices as variant

Sub someroutine

dim onedevice as variant

redim devices(index)

for each onedevice in devices
onedevice.getvalue
next

*******************

The problem seems to be that the object is not initialized so the program errors out. I can make it work by inserting a line "set onedevice = new device" into the loop, but that initializes the object to a 'zero' state and I can't access the value of the object in the array. I suppose I can use a for x = 1 to somelength version, but the for next should work, I think, so I would rather understand what I am doing wrong than work around it.

Any help would be greatly appreciated.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
I'll start.

Where's the 'array of user defined objects'?

onedevice is type variant. From where does it acquire a getvalue method?

Redim devices(index) clears the devices array unless you use Preserve. Did you intend that? (I assume index is initialised.)
 
Upvote 0
I am trying to loop through an array of user defined objects (classes) and am experiencing some difficulties. The code skeleton is as follows.

*************************
dim devices as variant

Sub someroutine

dim onedevice as variant

redim devices(index)

for each onedevice in devices
onedevice.getvalue
next

*******************

The problem seems to be that the object is not initialized so the program errors out. I can make it work by inserting a line "set onedevice = new device" into the loop, but that initializes the object to a 'zero' state and I can't access the value of the object in the array. I suppose I can use a for x = 1 to somelength version, but the for next should work, I think, so I would rather understand what I am doing wrong than work around it.

Any help would be greatly appreciated.

From what I can see the devices array is empty prior to initiating the loop so how can you expect to access the value of the objects .
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,331
Members
452,636
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top