Cancel without Dim?

I am using Access 2010 under Win7. I found that I can measure my arrays at runtime simply by calling 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 a reason I should NOT use this shortcut?

Hooray!

+4
source share
1 answer

It is interesting. This MSDN page confirms what you see: here is a quote:

“You can use the ReDim operator to declare an array implicitly inside a procedure. Be careful not to miss the array name when you use the ReDim operator. Even if the Option Explicit statement included in the module creates a second array.

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

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

As for your question, if you do, I would say no, because it is confusing and opens up code for errors that Option Explicit will not catch.

Redim Preserve enough, Redim Preserve does not exhibit this behavior.

+7
source

Source: https://habr.com/ru/post/1439950/


All Articles