Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Object Array Question

28 views
Skip to first unread message

steve...@yahoo.com

unread,
Jan 23, 2013, 3:26:43 PM1/23/13
to
Hi,

I was wondering if this approach would work, or if it is
even possible to generate an array of objects with VBScript.

I won't have the opportunity to try this for over a day; so
I thought I would ask you experienced folks before that time.

I want to create an array of Dictionary objects. I will not
know the number of instances until I get feedback from the
user.

Will this work?

Dim myObjArray
Dim iCount

myObjArray=Array(CreateObject("Scripting.Dictionary"))
iCount=5
ReDim myObjArray(iCount)

Thanks!

~Steve

Mayayana

unread,
Jan 23, 2013, 7:59:51 PM1/23/13
to
| Will this work?
|

No. You can add objects to an array, but you can't
do it that way. You need to add them one at a time.
But it's very unlikely that's the best way to achieve
whatever you're trying to do.

Tom Lavedas

unread,
Jan 23, 2013, 8:02:01 PM1/23/13
to
First, dynamic arrays cannot be declared the way you have done it. They are declared dynamic by adding a pair of parens in the Dim statement, thus, ...

Dim myObjArray()

Then they cannot be filled using the Array function. Rather they must be ReDimed to some size and information stored in them by addressing their elements, thus ...

ReDim Preserve myObjArray(0)
myObjArray(0) = somevar ' it can be a Dictionary, string or other object

Further, adding elements to the array will not make the new one(s) dictionary object(s). The elements will be of type variant. If you want then to be subtyped as Dictionary objects, you will need to define them as such, as you did with the first one. Each new element will need to be a newly created dictionary object, as in ...

ReDim Preserve myObjArray(1)
myObjArray(1) = createobject("Scripting.Dictionary")

Note the use of the Preserve keyword to keep the values of the previously defined elements.

HTH,
__________________________
Tom Lavedas
Message has been deleted

steve...@yahoo.com

unread,
Jan 24, 2013, 1:38:26 PM1/24/13
to
Thanks Tom and everyone.

I've now had the chance to implement this. Based upon what Tom said, plus I stumbled upon the fact that I also needed to use the "Set" statement, I was able to get an unpredetermined array count of dictionary objects:

Dim dictObj()
Dim lCtr, lCount

'User provides count via dialog box
For lCtr=0 To lCount
ReDim Preserve dictObj(lCtr)
Set dictObj(lCtr)=CreateObject("Scripting.Dictionary")
Next

Best Regards,

~Steve
- - - - - - - - - - - - - - - - - - - -
0 new messages