vba VB真的不区分大小写吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2300983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is VB really case insensitive?
提问by Todd Main
I'm not trying to start an argument here, but for whatever reason, it's typically stated that Visual Basic is case insensitive and C languages aren't (and somehow that is a good thing).
我不想在这里开始争论,但无论出于何种原因,通常都说 Visual Basic 不区分大小写,而 C 语言不是(不知何故,这是一件好事)。
But here's my question: Where exactly is Visual Basic case insensitive? When I type...
但这是我的问题:Visual Basic 不区分大小写究竟在哪里?当我打字...
Dim ss As String
Dim SS As String
...into the Visual Studio 2008or Visual Studio 2010IDE, the second one has a warning of "Local variable SSis already declared in the current block". In the VBA VBE, it doesn't immediately kick an error, but rather just auto-corrects the case.
...进入Visual Studio 2008或Visual Studio 2010IDE,第二个有警告“本地变量SS已在当前块中声明”。在 VBA VBE 中,它不会立即引发错误,而只是自动更正情况。
Am I missing something here with this argument that Visual Basic is not case sensitive? (Also, if you know or care to answer, why would that be a bad thing?)
我是否在这里错过了 Visual Basic 不区分大小写的论点?(此外,如果您知道或愿意回答,那为什么会是一件坏事?)
Why am I even asking this question?
我为什么要问这个问题?
I've used Visual Basic in many of its dialects for years now, sometimes as a hobbyist, sometimes for small business-related programs in a workgroup. As of the last six months, I've been working on a big project, much bigger than I anticipated. Much of the sample source code out there is in C#. I don't have any burning desire to learn C#, but if there are things I'm missing out on that C# offers that Visual Basic doesn't (an opposite would be VB.NET offers XML Literals), then I'd like to know more about that feature. So in this case, it's often argued that C languages are case sensitive and that's good and Visual Basic is case insensitive and that is bad. I'd like to know...
多年来,我一直在许多方言中使用 Visual Basic,有时作为业余爱好者,有时用于工作组中与小型企业相关的程序。在过去的六个月里,我一直在做一个大项目,比我预期的要大得多。大部分示例源代码都是用 C# 编写的。我没有任何学习 C# 的强烈愿望,但是如果我错过了 C# 提供的 Visual Basic 没有的东西(相反的是 VB.NET 提供XML 文字),那么我想要了解更多关于该功能的信息。因此,在这种情况下,经常有人认为 C 语言区分大小写,这很好,而 Visual Basic 不区分大小写,这很糟糕。我想知道...
- How exactly is Visual Basic case insensitive since every single example in the code editor becomes case sensitive (meaning the case gets corrected) whether I want it or not.
- Is this compelling enough for me to consider moving to C# if VB.NET case is somehow limiting what I could do with code?
- 由于代码编辑器中的每个示例都区分大小写(意味着大小写得到更正),无论我是否愿意,Visual Basic 的大小写到底有多不敏感。
- 如果 VB.NET 案例以某种方式限制了我可以用代码做什么,这是否足以让我考虑转向 C#?
采纳答案by MarkJ
The difference between VBA and VB.NETis just because VB.NET compiles continuously in the background. You'll get an error when you compile the VBA.
VBA 和VB.NET的区别只是因为 VB.NET 在后台持续编译。编译 VBA 时会出现错误。
Like Jonathan says, when programming you can think of VB.NET as case-insensitive apart from string-comparisons, XML, and a few other situations...
就像乔纳森说的那样,在编程时,除了字符串比较、XML 和其他一些情况之外,您可以将 VB.NET 视为不区分大小写...
I think you're interested in what's under the hood. Well, the .NET Common Language Runtime is case-sensitive, and VB.NET code relies on the runtime, so you can see it must be case-sensitive at runtime, e.g. when it's looking up variables and methods.
我想你对引擎盖下的东西很感兴趣。好吧,.NET 公共语言运行时是区分大小写的,而 VB.NET 代码依赖于运行时,因此您可以看到它在运行时必须区分大小写,例如,在查找变量和方法时。
The VB.NET compiler and editor let you ignore that - because they correct the casein your code.
VB.NET 编译器和编辑器让您可以忽略这一点 - 因为它们在您的代码中更正了这种情况。
If you play around with dynamic features or late-binding (Option Strict Off) you can prove that the underlying run-time is case-sensitive. Another way to see that is to realise that case-sensitive languages like C# use the same runtime, so the runtime obviously supports case-sensitivity.
如果您使用动态功能或后期绑定(Option Strict Off),您可以证明底层运行时区分大小写。另一种查看方式是意识到像 C# 这样区分大小写的语言使用相同的运行时,因此运行时显然支持区分大小写。
EDITIf you want to take the IDE out of the equation, you can always compile from the command-line. Edit your code in Notepadso it has ssand SSand see what the compiler does.
编辑如果你想把 IDE 排除在外,你总是可以从命令行编译。编辑代码在记事本,因此具有ss和SS看编译器做什么。
EDITQuote from Jeffrey Richterin the .NET Framework Design Guidelinespage 45.
编辑引用自杰弗里里希特在.NET Framework设计准则第45页。
To be clear, the CLR is actually case-sensitive. Some programming languages, like Visual Basic, are case insensitive. When the Visual Basic compiler is trying to resolve a method call to a type defined in a case-sensitive language like C#, the compiler (not the CLR) figures out the actual case of the method's name and embeds it in metadata. The CLR knows nothing about this. Now if you are using reflection to bind to a method, the reflection APIs do offer the ability to do case-insensitive lookups. This is the extent to which the CLR offers case-insensitivity.
需要明确的是,CLR 实际上是区分大小写的。一些编程语言,如 Visual Basic,不区分大小写。当 Visual Basic 编译器尝试将方法调用解析为以区分大小写的语言(如 C#)定义的类型时,编译器(而不是 CLR)会计算出方法名称的实际大小写并将其嵌入元数据中。CLR 对此一无所知。现在,如果您使用反射绑定到方法,反射 API 确实提供了进行不区分大小写查找的能力。这是 CLR 提供不区分大小写的程度。
回答by JaredPar
Part of the problem here is you need to divide the language from the IDE experience.
这里的部分问题是您需要将语言与 IDE 体验分开。
As a language, VB.NETis certainly a case insensitive with respect to identifiers. Calling DateTime.Parseand datetime.parsewill bind to the exact same code. And unlike languages like C#, it is not possible to define methods or types which differ only by case.
作为一种语言,VB.NET肯定对标识符不区分大小写。调用DateTime.Parse和datetime.parse将绑定到完全相同的代码。与 C# 等语言不同,无法定义仅因大小写而异的方法或类型。
As an IDE, VB.NET attempts to preserve the case of existing identifiers when it pretty lists a block of code. Pretty lists occur whenever you move off of the current logical line of code. In this case you move off of the second declaration of SS, the pretty lister notices there is an existing identifier with that name and corrects it to have matching case.
作为 IDE,VB.NET 在漂亮地列出代码块时尝试保留现有标识符的大小写。每当您离开当前的逻辑代码行时,就会出现漂亮的列表。在这种情况下,您从 的第二个声明中移开SS,漂亮的列表器会注意到存在具有该名称的标识符,并更正它以具有匹配的大小写。
This behavior, though, is purely done as a user value add. It is not a part of the core language.
但是,这种行为纯粹是作为用户增值而完成的。它不是核心语言的一部分。
回答by Jonathan Allen
VB is mostlycase insensitive, but there are exceptions. For example, XML literals and comprehension is case sensitive. String comparisons are usually case sensitive, unlike say T-SQL, but there are compiler switch to make string comparisons case insensitive. And of course there are the edge cases when dealing with inheritance, COM, and Dynamic Language Runtime.
VB大多不区分大小写,但也有例外。例如,XML 文字和理解是区分大小写的。字符串比较通常区分大小写,不像 T-SQL,但有编译器开关使字符串比较不区分大小写。当然,在处理继承、COM 和动态语言运行时时也存在边缘情况。
回答by Hans Passant
Yes, the VB.NET compiler treats identifiers in a case insensitive way. And yes, that can cause problems when it consumes assemblies that were written in another language or uses COM components. The former case is covered by the Common Language Specification. The relevant rule is:
是的,VB.NET 编译器以不区分大小写的方式处理标识符。是的,当它使用用另一种语言编写的程序集或使用 COM 组件时,这可能会导致问题。前一种情况包含在公共语言规范中。相关规则是:
For two identifiers to be considered distinct, they must differ by more than just their case.
对于要被视为不同的两个标识符,它们的不同必须不仅仅在于它们的大小写。
The COM case is rather crudely taken care of by the type library builder, it forces the casing of identifiers with the same name to be identical. Even when those identifiers have different roles. In other words, a method parameter with the name "index" will force a method name "Index" to be recased to "index". That has produced rather a lot of head scratching, as you might imagine :)
类型库构建器对 COM 情况进行了相当粗略的处理,它强制具有相同名称的标识符的大小写相同。即使这些标识符具有不同的作用。换句话说,名称为“index”的方法参数将强制将方法名称“Index”改写为“index”。正如您可能想象的那样,这已经产生了相当多的头疼:)
回答by Andrew Neely
VB is case preserving(in the IDE) but case insensitive. It's like Windows file system in a way. Hello.txt and hello.txt are considered to be the same file name.
VB保留大小写(在 IDE 中)但不区分大小写。在某种程度上,它就像 Windows 文件系统。Hello.txt 和 hello.txt 被认为是相同的文件名。
The IDE assumes that the declaration a variable is the "correct" case for that variable, and adjusts every instance of that variable match the declaration. It does this for eye-candy and consistency reasons, but not for functionality.
IDE 假定变量声明是该变量的“正确”情况,并调整该变量的每个实例以匹配声明。它这样做是出于美观和一致性的原因,而不是为了功能。
I've seen several instances where the case was not automatically changed to match the declaration, and the statement works just the same. You can also use any text editor to write code that will compile just fine in different cases.
我见过几个实例,其中大小写没有自动更改以匹配声明,并且语句的工作原理相同。您还可以使用任何文本编辑器编写在不同情况下都能很好编译的代码。
A side-note:
旁注:
Most PEOPLEthink in a case-insensitive manner. When we see the word "dog" the word is translated into meaning in our minds. The meaning of the word is not based upon case (i.e. regardless of if spell it "DOG", "DoG", or "dOG" still barks.) COMPUTERSsee words as discrete bags of bits. Uppercase and lowercase are different bit patterns, and are thus different.
多数人民认为,在不区分大小写的方式。当我们看到“狗”这个词时,这个词在我们的脑海中被翻译成意义。这个词的含义不基于大小写(即无论拼写“DOG”、“DoG”还是“dOG”仍然吠叫。) 计算机将词视为离散的比特包。大写和小写是不同的位模式,因此是不同的。
Since most programmers are human, case insensitivity seems more adapted to the way people think and case sensitivity is more about humans adapting how they think to the constraints of a machine.
由于大多数程序员都是人,因此不区分大小写似乎更适合人们的思考方式,而区分大小写更多地是关于人类如何根据机器的约束调整他们的思维方式。
回答by Sarfraz
This is part of the editor you are using, they may behave differently but the fact is that Visual Basic really iscase-insensitive language. So, ssand SSare same.
这是您正在使用的编辑器的一部分,它们的行为可能有所不同,但事实是 Visual Basic确实是不区分大小写的语言。所以,ss和SS是一样的。
Please have a look at VB.NET Basics tutorial for more information :)
回答by Michael Stum
I'm not sure I understand you? VB is case insensitive, so ss and SS is the same variable, so the compiler correctly complains that you re-declared the variable.
我不确定我理解你吗?VB 不区分大小写,所以 ss 和 SS 是同一个变量,所以编译器正确地抱怨你重新声明了变量。
I think that Variables are not case sensitive, but function names are.
我认为变量不区分大小写,但函数名称是。
回答by Xorlev
Yes, VB is case insensitive. It sometimes throws those not used to it for a bit of a loop.
是的,VB 不区分大小写。它有时会将那些不习惯的人抛出一个循环。
回答by supercat
One doesn't have to try all that hard in VB.NET to create code with different uppercase/lowercase "spellings" of an identifier. Changing the casing of an identifier in the file where it's declared without using the "Rename" function will not cause the name to be updated in other files, though editing any line which contains the name will cause it to conform to the present definition.
人们不必在 VB.NET 中尝试所有的努力来创建具有标识符的不同大写/小写“拼写”的代码。在不使用“重命名”功能的情况下更改声明标识符的文件中的标识符的大小写不会导致其他文件中的名称更新,尽管编辑包含该名称的任何行将使其符合当前定义。
In this way, one can determine that VB.NET is mostly case insensitive, but it does make the case of identifiers available to the CLR which may use that information in case-sensitive ways.
通过这种方式,可以确定 VB.NET 大多数情况下不区分大小写,但它确实使 CLR 可以使用标识符的大小写,CLR 可能会以区分大小写的方式使用该信息。
回答by htm11h
I can only offer this, which as I recall from my programming text books back in the early 80s, is that case senstive languages were, (at that time) strictly intended to reduce compile time errors. That is to say, the "strictness" was intended to develop a coding discipline of greater accuracy. As it has turned out the addition of proper labeling of variables, classes, methods, functions, and whatever else you wish to throw in there, evolved as well.
我只能提供这一点,正如我在 80 年代初的编程教科书中所回忆的那样,区分大小写的语言(当时)严格旨在减少编译时错误。也就是说,“严格”是为了发展一个更准确的编码规则。事实证明,添加适当的变量、类、方法、函数以及您希望放入的任何其他标签的标签也得到了发展。
I recall almost all of those books included a recommended pattern for leading capitalization, lower case, etc. As we all know, much of that has been thrown out or should I say, ignored in practice, save for the high end production houses, and CASE solutions, or for those that have reached a higher skill level. I think everyone experiences this learning curve.
我记得几乎所有这些书都包含推荐的首字母大写、小写字母等模式。众所周知,除了高端制作公司外,其中大部分已被丢弃,或者我应该说,在实践中被忽略了,以及CASE 解决方案,或针对那些已达到更高技能水平的解决方案。我认为每个人都经历过这种学习曲线。
Given the advancement of these langauges and IDE's, the better question becomes, which language improves my dev time? Of course if you are not familiar with each of the various langs, your options are limited.
鉴于这些语言和 IDE 的进步,更好的问题变成了,哪种语言可以改善我的开发时间?当然,如果您不熟悉各种语言中的每一种,那么您的选择是有限的。

