REDIM没有有啥用? [英] Redim without Dim?

查看:130
本文介绍了REDIM没有有啥用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用的Win7下的Access 2010。我发现我可以简单地调用在运行时维数组我使用ReDim arrayName中(X)没有先声明数组作为暗淡arrayName中的()

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?

干杯!

推荐答案

这很有趣。 这个MSDN页面确认你所看到的:这里有一个报价

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

您可以使用ReDim语句隐含内声明数组
  的过程。要小心,不要拼错数组的名称你的时候
  使用ReDim语句。即使Option Explicit语句是
  模块中包括的,第二阵列将创建

"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."

这页解释了 REDIM 创建一个新的阵列与现有阵列复制到它(假如有):

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 /库/ w8k3cys2%28V = VS.80%29.aspx

至于你的问题,你应该做的,我会说不,因为它的混乱,不打开code到显式的选项不会捕获错误。

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.

不够合理, REDIM preserve 不出现此行为。

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

这篇关于REDIM没有有啥用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆