了解 Visual Basic 中的变量
如果我不声明变量的类型,主要问题是什么? 例如,Dim var1
与 Dim var1 as Integer
。
What's the main problem if I don't declare the type of a variable? Like, Dim var1
versus Dim var1 as Integer
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在允许使用变体类型的语言中声明变量类型的主要原因是为了对自己进行检查。 如果您有一个用于保存字符串的变量,然后不小心将其传递给需要整数的函数,则编译器无法告诉您您搞砸了,除非您告诉it该变量应该始终是一个字符串。 相反,您的字符串被重新解释为整数,这几乎永远不会给您想要的结果,结果可能会令人困惑,并且很难追踪到错误。
在几乎所有语言中,都有很多结构,您可以将其省略,并且您的程序可以工作,但存在作为对程序员的检查。 编译器的第一个工作是将代码变成可执行文件。 但编译器的第二个工作是尽可能地尝试确保程序员没有犯错误。 特别是当你的程序变得很大时,让编译器更容易发现这样的错误,而不是相信你输入的所有内容都完全正确。
此外,通常还会有一些与变体相关的处理开销,但这是一个较小的问题。
The main reason to declare variable types in languages which allow you to use variant types is as a check on yourself. If you have a variable that you're using to hold a string, and then by accident you pass it to a function that expects an integer, the compiler can't tell you that you messed up unless you told it that that variable was supposed to always be a string. Instead, you're stuck with your string being reinterpreted as an integer, which is pretty much never going to give you what you want and the results will likely be confusing, and it will be hard to track down the bug.
In pretty much all languages there are a lot of constructs where you could leave it out and your program would work, but exist as a check on the programmer. The first job of a compiler is to make your code into an executable. But the second job of the compiler is to try as much as possible to make sure that the programmer didn't make a mistake. Especially when your program gets large, it's easier to let the compiler find mistakes like this as opposed to trusting that you typed everything exactly right.
Additionally, there is usually some processing overhead associated with variants, but this is a more minor concern.
有几个原因:
Variant
类型之间进行转换的成本虽小,但并非微不足道。lblTitle
告诉您某物应该带有Label
)。There are a few reasons:
Variant
types has a small but nontrivial cost.lblTitle
to tell you that something is supposed to hold aLabel
).其他人已经提到了 Intellisense,但值得重复一下。
此外,当您为变量声明显式类型时,编译器可以对代码进行各种额外的类型检查和验证,否则这是不可能的。 现在发生的情况是,某些类型的非常常见的错误在编译时而不是运行时被捕获并修复。 用户永远不会看到它们。 您不想在运行时留下错误。
你说“它可以是任何东西”——这是真的。 但你接着说“所以它一定很好”。 但这并不一定是这样,而且这是非常危险的。 并非所有内容都可以分配或与其他内容组合。 它可以是任何东西——但它是某种东西,或者更确切地说是某种特定的东西。 如果没有显式类型,编译器就无法知道什么,也无法帮助您避免错误。
Someone else already mentioned Intellisense, but but it's worth repeating.
Additionally, when you declare an explicit type for your variable you make it possible for the compiler to do all kinds of extra type checking and validation on your code that would not otherwise be possible. What happens is that now certain kinds of very common error are caught and fixed at compile time rather than run time. The user never sees them. You don't want to leave errors for run-time.
You say "it could be anything" — and that is true. But you then go on to say "so it must be good". That doesn't necessarily follow, and it's very dangerous. Not everything can be assigned or combined with everything else. It could be anything — but it is something, or rather some specific thing. Without an explicit type, the compiler has no way to know what and can't help you avoid errors.
从表面上看,如果你不声明类型,Intellisense 无法帮助你,因为它不知道它是什么类型。
Superficially, if you don't declare the type, Intellisense can't help you with it because it doesn't know what type it is.
将此与 Python 的类型系统进行对比。 Python 允许开发人员在不提前声明类型的情况下使用变量,但一旦使用变量,类型就固定了。 相比之下,变体最初可以被分配任何类型的值,并且稍后可以存储不同的类型,而不会出现任何警告或投诉。 因此,您可以将字符串放入先前保存数字的变量中。
如果您曾经需要维护别人的代码,您将开始理解为什么这种事情(默默地更改变量类型)可能会特别难以维护。 特别是如果您使用模块级变量,这可能会导致一些非常有趣的问题。 这与在 VB 代码中使用 Option Explicit 的思路相同。 如果没有 Option Explicit,您可能会在没有意识到的情况下执行此类操作:
在某些字体中,这两个变量名称将无法区分,这可能会导致一些难以发现的错误。
Contrast this to Python's typing system. Python allows a developer to use a variable without declaring type in advance but once a variable is used, the type is fixed. A variant, by contrast, can be assigned any type of value initially and a different type can be stored later on without any warning or complaint. So you can put a string into a variable that previously held a numeric.
If you ever have to maintain someone else's code, you'll start to understand why this sort of thing (changing a variable type silently) can be extra tough to maintain. Especially if you're using a module level variable this could lead to some really interesting problems. This is along the same lines as using Option Explicit in VB code. Without Option Explicit you can do this sort of thing without realizing it:
In some fonts those two variable names would be impossible to distinguish and this could lead to some tough to find bugs.