为什么我应该真正在VBA中确定变量的尺寸? [英] Why should I dimension my variables in VBA really?

查看:84
本文介绍了为什么我应该真正在VBA中确定变量的尺寸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

他们曾经说过这与内存管理有关. 但是,看一看我发现无法对变量进行尺寸标注会将其默认为分配22个字节内存的变量数据类型.

They used to say it was about memory management. However, having a look at things I find that failure to dimension a variable will default it to a variant data type which allocates 22 bytes of memory.

让我们考虑仅一个兆字节的内存. 1,000,000字节. 这意味着我将需要45,454个变量才能咀嚼单个MB的内存.大多数现代系统都有几个GB的内存.所以问题是我真的应该在乎为所有变量仔细设置正确的暗淡吗?还是按照现代标准浪费时间?

Let's consider just a single megabyte of memory. 1,000,000 bytes. This means I would require 45,454 variables just to chew through a single MB of memory. Most modern systems have several GB of memory. So the question is should I really care about carefully setting the right dim for all of my variables? Or is it a waste of time by modern standards?

推荐答案

内存使用只是一个令人反感的副作用.我建议使用Option Explicit的真正原因是,它允许编译器保护您免受编译和运行时出现的编码错误的影响,但不会执行您打算做的事情.

Memory usage is only pleasant a side effect. The real reason I'd recommend using Option Explicit is that it allows the compiler to protect you from coding mistakes that compile and run, but don't do what you intend.

示例1:可以防止错别字变成意外的灾难.

Example 1: It keeps typos from turning into unexpected disasters.

Dim variableNamedFoobar As String
variableNamedFoobar = "Something"

If varableNamedFoobar <> "Something" Then
    Debug.Print "Did you catch that? The compiler would..."
End If

如果您未明确声明变量,则编译器会愉快地为您提供22字节的空字符串以供比较.

If you aren't explicitly declaring variables, the compiler happily gives you a 22 byte empty string to compare against.

示例2:它还可以防止范围泄漏.考虑一个具有多个可变范围级别的大型项目.希望您记得您的全球客户是什么:

Example 2: It also protects against leaking scope. Consider a huge project with multiple levels of variable scope. Hope you remember what your globals are:

Private x As Integer

Private Sub First()
    'I remembered here that x is a global.
    x = 2
    Debug.Print x
    Second
    Debug.Print x
End Sub

Private Sub Second()
    'I forgot here.
    For x = 1 To 5
    Next x
End Sub

添加Dim x as Integer用作循环计数器可确保将其范围限定为Second().

Adding Dim x as Integer for use as the loop counter ensures that it is scoped to Second().

示例3::它允许编译器检测类型不匹配.

Example 3: It allows the compiler to detect type mismatches.

Set foo = New Collection
foo.Add "Bar"
Debug.Print TypeName(foo)

'... 100 lines of code later...

foo = 6
Debug.Print TypeName(foo)

如果某处有Dim foo As Collection,则会出现编译时错误,告知您已经一个foo,并且不应为其分配6.

If you had Dim foo As Collection in there somewhere, you get a compile time error letting you know that you already had a foo and you shouldn't be assigning 6 to it.

还有许多其他示例,其中您可以使用隐式变量声明在脚(或更高位置)上射击自己.尽管这些示例在上面的代码块隔离中很容易发现,但是它们中的任何一个都可能在大型代码库中引起微妙且极其难以调试的错误.帮自己一个忙(以及以后可能需要维护您代码的每个人),然后键入多余的代码行.

There are lots of other examples of where you can shoot yourself in the foot (or higher) with implicit variable declarations. While these examples are easy to spot in the isolation of the code blocks above, any one of them can cause subtle and extremely difficult to debug errors in a large code base. Do yourself (and everyone that might need to maintain your code later) a favor and type the extra line of code.

这篇关于为什么我应该真正在VBA中确定变量的尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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