Hi.

I have the following code:

Dim stringArray(0) As String
ReDim Preserve stringArray(1)

When I try to run it, I get the following error:

Compile error:
Array already dimensioned

What is the meaning of this? I thought that ReDim is the way to
redimension an array in VBScripting? Does anyone have any thoughts on
this?

Thanks

--
Jan Eliasen, representing himself and not the company he works for.

Re: ReDim by JGM

JGM
Thu Dec 04 16:13:50 CST 2003

Hi Jan,

To add to what Malcom said.... I have just recently learned to use arrays
(up to 3 dimensions for now!), so explaining to you (or to anybody else
reading this thread wondering about arrays!) what I know will help me
consolidate my knowledge, and also, if anyone notices that I am way off
somehow, please, let me know.

Arrays are very useful because you can store almost anything in them, and it
is fairly easy to retrieve the information later, especially if you pass the
array variable between subs or functions. OTH, very often you do not know
the array size until some loop or other similar counting procedure or code
determines its size.

If you know at the start the array size, then you do not need Redim, you
just use something like:

'_______________________________________
Dim i As Integer
Dim MyArrayTest(4) As String
'note that
'MyArrayTest(4) = MyArrayTest(0 to 4), or 5 rows
'and MyArrayTest(1 to 5) also equals 5 rows...
For i = 0 To 4
MyArrayTest(i) = "My item # " & i & "."
Next i
'_______________________________________

Now, if your code is going to determine the size, then I find that there are
two situations.
1) You have a way of determining the size before you fill it in, like if you
were building an array to contain all bookmark names in a document:

'_______________________________________
Dim i As Integer
Dim MyArrayTest() As String
Dim ItemCount As Integer

ItemCount = ActiveDocument.Bookmarks.Count

'use Redim here to size the array before filling it
'Start the array at 1 because the first bookmark index is 1
ReDim MyArrayTest(1 To ItemCount)

For i = 1 To ItemCount
MyArrayTest(i) = "Bookmark " & i & " name: " & _
ActiveDocument.Bookmarks(i).Name
Next i
'_______________________________________

or

2) You do not know the size beforehand, your code will look at a list of
items and select/discard some items based on some condition(s).
Here, what I do is fix the size before I start with a number that I know is
higher than (or equal to) the maximum I need, then whem I am done, I resize
the array. You can incude defensive programming to make sure that the user
is not in a situation where that pre-determined over-sized number is in fact
larger than necessary. You have to do that otherwise, if you do not fix an
array size, you cannot fill it in. Here you need Redim and redim Preserve:

'_______________________________________
Dim i As Integer
Dim IncludedItems As Integer
Dim MyArrayTest() As String
Dim ItemCount As Integer

ItemCount = ActiveDocument.Bookmarks.Count
IncludedItems = 0

'Maximum possible
ReDim MyArrayTest(1 To ItemCount)

For i = 1 To ItemCount
If InStr(ActiveDocument.Bookmarks(i).Name, "Test") <> 0 Then
MyArrayTest(i) = "Bookmark " & i & " name: " & _
ActiveDocument.Bookmarks(i).Name
IncludedItems = IncludedItems + 1
End If
Next i

'Resize to actual maximum, use Preserve otherwise the array
'content will be flushed
ReDim Preserve MyArrayTest(1 To IncludedItems)
'_______________________________________

In a nutshell, this is what I have understood about arrays in the last few
months.

HTH
Cheers!
--
_______________________________________
Jean-Guy Marcil
jmarcil@sympatico.ca

"Jan Eliasen" <spam@eliasen.dk> a écrit dans le message de news:
adiusvglfcgajbl30olsircsb28h71m4a5@4ax.com...
> Hi.
>
> I have the following code:
>
> Dim stringArray(0) As String
> ReDim Preserve stringArray(1)
>
> When I try to run it, I get the following error:
>
> Compile error:
> Array already dimensioned
>
> What is the meaning of this? I thought that ReDim is the way to
> redimension an array in VBScripting? Does anyone have any thoughts on
> this?
>
> Thanks
>
> --
> Jan Eliasen, representing himself and not the company he works for.



Re: ReDim by Jan

Jan
Fri Dec 05 01:33:43 CST 2003

On Thu, 4 Dec 2003 15:14 +0000 (GMT Standard Time),
malcol.smith@droganqdrup.com (Malcolm Smith) wrote:

>You should have declared the array as being:
>Dim stringArray() as String
Right. Thanks. I actually started out doing that, but then I ran into
another problem.

I wanted to have a loop, in which I Redim'ed the array to have one
more place and then I filled it with data. But I couldn't find a way
to do that. If I use
Dim stringArray() As String

then I can't do this:
ReDim Preserve stringArray(UBound(stringArray) + 1) because UBound
will fail. How do I check if stringArray contains any elements (empty
or not)?

>Now, there is something in your text which is slightly of concern. You
>mention VBScripting. Do you mean VBScript or do you mean VBA? If you
>are using Word's language then this is VBA and not VBScript.
Yes, I am sorry about the confusion. I am using VBA.

--
Jan Eliasen, representing himself and not the company he works for.

Re: ReDim by JGM

JGM
Fri Dec 05 08:07:43 CST 2003

Hi Jan,

Have you tried (if it is possible in your case.. I do not know how you
select what you are trying to store in the array...) something like:

'_______________________________________
Dim stringArray() As String

Redim stringArray(5000)

'do stuff to fill the array...
'and have a counter increment everytime you add something to the array
'Let's call it ArrayCount
ArrayCount = ArrayCount + 1

ReDim Preserve stringArray(ArrayCount)
'_______________________________________

Or, maybe you can use a boolean like

'_______________________________________
Dim NotInArrayYet as Boolean
Dim stringArray() As String

'use that boolean like this:

NotInArrayYet = True

'Start loop to fill the array
'then, with the code that puts an element in the array
If NotInArrayYet Then
ReDim Preserve stringArray(0)
stringArray(0) = 'an item to add to the array
NotInArrayYet = False
Else
ReDim Preserve stringArray(UBound(stringArray) + 1)
stringArray(UBound(stringArray)) = 'an item to add to the array
End If
'Next ...
'_______________________________________

Thanks for the post... I learned another way of controlling an array!

HTH
Cheers!
--
_______________________________________
Jean-Guy Marcil
jmarcil@sympatico.ca

"Jan Eliasen" <spam@eliasen.dk> a écrit dans le message de news:
62d0tv892026pd896mmh4u7p7lcl9ra7t1@4ax.com...
> On Thu, 4 Dec 2003 15:14 +0000 (GMT Standard Time),
> malcol.smith@droganqdrup.com (Malcolm Smith) wrote:
>
> >You should have declared the array as being:
> >Dim stringArray() as String
> Right. Thanks. I actually started out doing that, but then I ran into
> another problem.
>
> I wanted to have a loop, in which I Redim'ed the array to have one
> more place and then I filled it with data. But I couldn't find a way
> to do that. If I use
> Dim stringArray() As String
>
> then I can't do this:
> ReDim Preserve stringArray(UBound(stringArray) + 1) because UBound
> will fail. How do I check if stringArray contains any elements (empty
> or not)?
>
> >Now, there is something in your text which is slightly of concern. You
> >mention VBScripting. Do you mean VBScript or do you mean VBA? If you
> >are using Word's language then this is VBA and not VBScript.
> Yes, I am sorry about the confusion. I am using VBA.
>
> --
> Jan Eliasen, representing himself and not the company he works for.



Re: ReDim by Jan

Jan
Fri Dec 05 10:09:59 CST 2003

On Fri, 5 Dec 2003 09:07:43 -0500, "JGM" <no-spam@leaveme.alone>
wrote:

>Have you tried (if it is possible in your case.. I do not know how you
>select what you are trying to store in the array...) something like:
Yes, I suppose that could be done. It just seems funny - there must be
somehow to check for an empty array.

--
Jan Eliasen, representing himself and not the company he works for.

Re: ReDim by JGM

JGM
Fri Dec 05 10:57:33 CST 2003

Hi Jan,

I think, just for the sake of keeping things clear, when you say "empty
array," you mean "array with a Null size."

I mean, if you declare

Dim MyArray()

then it is not only "empty", but it has no size or dimension, it is 0x0, but
has the potential of being anything we want.

Whereas, if you declare

Dim MyArray()
ReDim MyArray(5000)

then now you have an empty array, but it has a size of 1column x 5000rows...

I think this is why I have always seen this way of working with array (the
Dim / Redim above), because I do not think there is a way of determining an
array dimension size by testing it, but if there is, I would really like to
know!
But in your case, since you create the array within your code and you fill
it in yourself, then you know its size at all times. So the first time
around, the size is Null, then you make it 1x1, put something in it, then
the second time around you know that it is 1x1, so you make it 1x2, and so
on...

Every example I have seen with arrays followed that pattern (Dim > ReDim >
ReDim Preserve), but from reading your post, I got the feeling you did not
want to go that way, especially when I saw the nice way by which you
dynamically resized the array at every pass, so this is whay I suggested the
Boolean trick...

Still, another way to go is as follows (you may find thai more aesthetically
pleasing!):

'_______________________________________
Dim myarray()

'Set starting size, i.e. 1x1
ReDim myarray(0)

For i = 0 To 4
myarray(i) = "item " & i
'increase size by one for next pass
ReDim Preserve myarray(UBound(myarray) + 1)
Next i

'remove last row that was added at the end of the last pass,
'but was not used because we got out of the For...Next

ReDim Preserve myarray(UBound(myarray) - 1)
'_______________________________________

Good luck!

--
_______________________________________
Jean-Guy Marcil
jmarcil@sympatico.ca

"Jan Eliasen" <spam@eliasen.dk> a écrit dans le message de news:
jdb1tvk67qbu7jbrh242v826cndfffhijd@4ax.com...
> On Fri, 5 Dec 2003 09:07:43 -0500, "JGM" <no-spam@leaveme.alone>
> wrote:
>
> >Have you tried (if it is possible in your case.. I do not know how you
> >select what you are trying to store in the array...) something like:
> Yes, I suppose that could be done. It just seems funny - there must be
> somehow to check for an empty array.
>
> --
> Jan Eliasen, representing himself and not the company he works for.



Re: ReDim by Jan

Jan
Mon Dec 08 01:57:10 CST 2003

On Fri, 5 Dec 2003 11:57:33 -0500, "JGM" <no-spam@leaveme.alone>
wrote:

>I think, just for the sake of keeping things clear, when you say "empty
>array," you mean "array with a Null size."
Yes, exactly

If I have this:
Dim ar() as String

Then I want to be able to do this:

if UBound(ar) = -1 Then
SomeCode
end if

Or something similar, but I can't find a way of testing for this.

>Dim myarray()
>
>'Set starting size, i.e. 1x1
>ReDim myarray(0)
>
>For i = 0 To 4
> myarray(i) = "item " & i
> 'increase size by one for next pass
> ReDim Preserve myarray(UBound(myarray) + 1)
>Next i
>
>'remove last row that was added at the end of the last pass,
>'but was not used because we got out of the For...Next
>
>ReDim Preserve myarray(UBound(myarray) - 1)
Yes, that is, of course, also possible - I am just running through the
array with a for-loop that goes from LBound to UBound-1

--
Jan Eliasen, representing himself and not the company he works for.