Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
986 views
in Technique[技术] by (71.8m points)

arrays - Redim without Dim?

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...