了解 Visual Basic 中的变量

发布于 2024-07-15 09:19:48 字数 89 浏览 7 评论 0原文

如果我不声明变量的类型,主要问题是什么? 例如,Dim var1Dim 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

怪我太投入 2024-07-22 09:19:48

在允许使用变体类型的语言中声明变量类型的主要原因是为了对自己进行检查。 如果您有一个用于保存字符串的变量,然后不小心将其传递给需要整数的函数,则编译器无法告诉您搞砸了,除非您告诉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.

初见 2024-07-22 09:19:48

有几个原因:

  • 大大提高了类型安全性。
  • 降低认知开销; 编译器和 Intellisense 可以帮助您。
  • 更低的性能开销; 在 Variant 类型之间进行转换的成本虽小,但并非微不足道。
  • 消除了命名疣的需要(例如,lblTitle 告诉您某物应该带有Label)。
  • 将某些类型的运行时错误移至编译时错误,这是生产力的巨大进步。

There are a few reasons:

  • Vastly improved type safety.
  • Lower cognitive overhead; the compiler and Intellisense can help you.
  • Lower performance overhead; transforming things to and from Variant types has a small but nontrivial cost.
  • Eliminates need for naming warts (e.g., lblTitle to tell you that something is supposed to hold a Label).
  • Moves some kinds of runtime errors to compile-time errors, which is a big productivity win.
财迷小姐 2024-07-22 09:19:48

其他人已经提到了 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.

美人如玉 2024-07-22 09:19:48

从表面上看,如果你不声明类型,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.

温暖的光 2024-07-22 09:19:48

将此与 Python 的类型系统进行对比。 Python 允许开发人员在不提前声明类型的情况下使用变量,但一旦使用变量,类型就固定了。 相比之下,变体最初可以被分配任何类型的值,并且稍后可以存储不同的类型,而不会出现任何警告或投诉。 因此,您可以将字符串放入先前保存数字的变量中。

Dim myvar1
myvar1 = 1

'A whole lot more code 

myvar1 = "this string"

如果您曾经需要维护别人的代码,您将开始理解为什么这种事情(默默地更改变量类型)可能会特别难以维护。 特别是如果您使用模块级变量,这可能会导致一些非常有趣的问题。 这与在 VB 代码中使用 Option Explicit 的思路相同。 如果没有 Option Explicit,您可能会在没有意识到的情况下执行此类操作:

myvar1 = 1

'A whole lot more code here too

myvarl = 2

在某些字体中,这两个变量名称将无法区分,这可能会导致一些难以发现的错误。

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.

Dim myvar1
myvar1 = 1

'A whole lot more code 

myvar1 = "this string"

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:

myvar1 = 1

'A whole lot more code here too

myvarl = 2

In some fonts those two variable names would be impossible to distinguish and this could lead to some tough to find bugs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文