4

I'm using Access 2010 under Win7. I've discovered I can dimension my arrays at run-time by simply invoking ReDim arrayName(x) without first declaring the array as Dim arrayName().

Sub FooBar()
   ReDim myArray(2)
   myArray(0) = "This is the first string in myArray."
   myArray(1) = "This is the second string in myArray."
   myArray(2) = "And this is the last string in myArray."
   MsgBox myArray(0) & vbCrLf & myArray(1) & vbCrLf & myArray(2)
End Sub

Is there any reason I should NOT use this shortcut?

Cheers!

DUHdley d'Urite
  • 187
  • 3
  • 15
  • Shortcut? `Redim myArray(0)` is 2 characters longer than `Dim myArray(0)`. – brettdj Oct 16 '12 at 03:52
  • But you can't dynamically dimension an array at run time. So, if you don't know the array size prior to compiling, I'm told that you're "supposed" to do this: `Dim myArray()` and then `ReDim myArray(x)` where 'x' is the integer/long value of the array size. – DUHdley d'Urite Oct 16 '12 at 04:06
  • That is correct. You hadn't mentioned the dynamic dimension part in your initial question. Rgds – brettdj Oct 16 '12 at 05:35

1 Answers1

7

That's interesting. This MSDN page confirms what you're seeing: Here's a quote:

"You can use the ReDim statement to declare an array implicitly within a procedure. Be careful not to misspell the name of the array when you use the ReDim statement. Even if the Option Explicit statement is included in the module, a second array will be created."

This page explains that Redim creates a new array and that the existing array is copied into it (assuming there is one):

http://msdn.microsoft.com/en-us/library/w8k3cys2%28v=vs.80%29.aspx

As to your question, should you do it, I'd say no, because it's confusing, and does open your code to errors that Option Explicit won't catch.

Reasonably enough, Redim Preserve doesn't exhibit this behavior.

brettdj
  • 54,857
  • 16
  • 114
  • 177
Doug Glancy
  • 27,214
  • 6
  • 67
  • 115
  • Thanks, Doug, for digging up the documentation. I don't want to confuse anyone, including myself, and so I **WAS** going to go ahead and add the explicit `Dim` statement. However, I could `Dim myArray` and then `ReDim myArary(x)`, and `Option Explicit` in this case won't protect me from a "_Subscript out of range_" run-time error. So, I think I'm actually safer from undefined variables if I skip the `Dim` and just declare and and dimension the array once with the `ReDim`. However, you did the legwork and offered your considered opinion, so I'll accept your answer. – DUHdley d'Urite Oct 16 '12 at 05:11
  • 1
    Thanks! I was thinking the same thing as what you're saying here and I agree that in this case Option Explicit won't protect you. I run M-Z Tools - http://www.mztools.com/v3/mztools3.aspx - and use its Review Source Code tool, which tells me if i have unused variables. So that might catch the misspelled Redim, assuming I never Redimmedd with the correct spelling. I salute (and +1) you for bringing up this interesting quirk. – Doug Glancy Oct 16 '12 at 13:42