Mark
asked on
how to redimension array
I have the following:
ReDim Preserve jobCodes(0 To jbMax) As String
and when I compile I get the compile error: Array Already Dimensioned
The message is true: my array *was* previously dimensioned. This is my frist shot at using dynamic arrays, so I'm sure I'm doing something wrong, but I thought one was supposed to use the "Preserve" when the arrary was previously dimensioned. What am I doing wrong?
ReDim Preserve jobCodes(0 To jbMax) As String
and when I compile I get the compile error: Array Already Dimensioned
The message is true: my array *was* previously dimensioned. This is my frist shot at using dynamic arrays, so I'm sure I'm doing something wrong, but I thought one was supposed to use the "Preserve" when the arrary was previously dimensioned. What am I doing wrong?
How was your array dimensioned originally? Was it also an array of Strings?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
> You do not need to DIM an array
That is true. Option Explicit will not catch the problem. Formally (from the VB help file), you should not use it that way:
The ReDim statement is used to size or resize a dynamic array that has already been formally declared using a Private, Public, or Dim statement with empty parentheses (without dimension subscripts).
The problem becomes apparent in the snippet. The typo isn't identified, and the code breaks only at run-time. I don't know why the VB compiler was made to imply Dim when reading ReDim, these are quite different operations, and no other language makes that particular confusion. Anyway, you are right:
ReDim Something(12) As String
Is interpreted as
Dim Something() As String
ReDim Something(12)
Stupid, but there you go...
(°v°)
That is true. Option Explicit will not catch the problem. Formally (from the VB help file), you should not use it that way:
The ReDim statement is used to size or resize a dynamic array that has already been formally declared using a Private, Public, or Dim statement with empty parentheses (without dimension subscripts).
The problem becomes apparent in the snippet. The typo isn't identified, and the code breaks only at run-time. I don't know why the VB compiler was made to imply Dim when reading ReDim, these are quite different operations, and no other language makes that particular confusion. Anyway, you are right:
ReDim Something(12) As String
Is interpreted as
Dim Something() As String
ReDim Something(12)
Stupid, but there you go...
(°v°)
Sub Manipulate(pvarMyArray() As Variant)
ReDim pvarMyAray(12)
pvarMyArray(12) = "something"
End Sub
Sub ShowProblem()
Dim Values() As Variant
Manipulate Values
End Sub
harfang, I always use Option Explicit and would always recoment it's use for trapping the logic error that are caused by a stupid typo. Everyone should have this option turned ON in your VBA Options (goodnes knows why the default is OFF)
Cheers, Andrew
Cheers, Andrew
ASKER
didn't implement any solutions