简体   繁体   English

在VB.NET中使用Dim foo As Foo有什么问题吗?

[英]Are there issues using Dim foo As Foo in VB.NET?

In a recent VB.NET project I adopted the naming conventions I'm used to using in C#. 在最近的VB.NET项目中,我采用了我习惯在C#中使用的命名约定。 Namely, often calling a variable the same name as the class it references, only with a different case, eg 也就是说,经常调用与它引用的类相同的变量,只有不同的情况,例如

Foo foo = new Foo(); // C#

Dim foo As New Foo() ' VB.NET

I find this is often the clearest way to write code, especially for small methods. 我发现这通常是编写代码的最清晰的方法,特别是对于小方法。 This coding style obviously works fine in C#, being case sensitive, and because of the syntax highlighting provided by Visual Studio, it is very easy to see that the class name and the variable name are different. 这种编码风格显然在C#中运行良好,区分大小写,并且由于Visual Studio提供的语法高亮,很容易看出类名和变量名是不同的。

However, to my surprise, this also worked fine nearly 100% of the time* in VB.NET. 然而,令我惊讶的是,在VB.NET中,这几乎在100%的时间内都能正常工作。 The only issue was that the variable name then appeared to take on a multiple identity. 唯一的问题是变量名称似乎具有多重身份。 Namely it could be used to call both instance methods and Shared (static) methods of the Foo class. 即它可以用于调用Foo类的实例方法和共享(静态)方法。 This didn't really cause any problems though, it just meant that Intellisense would provide a list containing both static and instance methods after you hit the '.' 这并没有真正引起任何问题,只是意味着Intellisense会在你点击''后提供包含静态和实例方法的列表。 after the variable name. 变量名后面。

I found, again to my surprise, that this didn't actually lead to any confusion in my project, and it's been very successful so far! 我惊讶地发现,这实际上并没有导致我的项目出现任何混乱,到目前为止它已经非常成功了! However I was the only person working on this particular project. 但是,我是唯一一个从事这个特定​​项目的人。

Here is a slightly longer example: 这是一个稍长的例子:

Dim collection as Collection = New Collection()
For Each bar As Bar in Bar.All()
    collection.SomeInstanceMethod(bar)
Next
collection.SomeSharedMethod()

* The only issue I found with this was that sometimes the 'Rename' refactoring tool got confused, ie when renaming a class it would rename the variables with the same name as the class as well, in their declaration lines ( Dim foo As... ), but not the other references to that variable, causing compiler issues (duh). *我发现的唯一问题是有时“重命名”重构工具变得混乱,即在重命名一个类时,它会重命名与类同名的变量,在它们的声明行中( Dim foo As... ),但不是对该变量的其他引用,导致编译器问题(duh)。 These were always easy to correct though. 这些总是很容易纠正。

Another small annoyance is that the VB.NET syntax highlighter doesn't highlight class names any differently than variable names, making it not quite as nice as when using it in C#. 另一个小烦恼是VB.NET语法高亮显示器没有突出显示类名与变量名的任何不同,使得它不如在C#中使用它时那么好。 I still found the code very readable though. 我仍然发现代码非常易读。

Has anyone else tried allowing this in a team environment? 还有其他人试图在团队环境中允许这个吗? Are there any other potential issues with this naming convention in VB.NET? 在VB.NET中这个命名约定是否还有其他潜在的问题?

Although VB is case-insensitive, the compiler is intelligent enough to not being confused between the object-instance and the class. 尽管VB不区分大小写,但编译器足够智能,不会在对象实例和类之间混淆。

However, it's certainly very dangerous and wrong to use the same name in a case-insensitive language! 但是,在不区分大小写的语言中使用相同的名称肯定是非常危险和错误的! Especially if other programmers are working on that project. 特别是如果其他程序员正在从事该项目。

I have to move back and forth between VB and C#, and we consider this poor practice. 我必须在VB和C#之间来回移动,我们认为这种做法很糟糕。 We also don't like letting variable names in C# differ from their type only by case. 我们也不喜欢让C#中的变量名与它们的类型区别开来。 Instead, we use an _ prefix or give it a more meaningful name. 相反,我们使用_前缀或给它一个更有意义的名称。

Whenever you start a new language it's inevitable you'll notice a bunch of things that are different and miss the old way of doing things. 每当你开始使用一种新语言时,你就会注意到一堆不同的东西会错过旧的做事方式。 Often this is because you are initially unaware of different features in the other language has that address the same problem. 这通常是因为您最初没有意识到其他语言中的不同功能可以解决同样的问题。 Since you're new to VB, here are a couple notes that will help you get things done: 由于您是VB的新手,所以这里有几个笔记可以帮助您完成工作:

It's not 100% correct to say that VB.Net is case-insensitive unless you also make the point that it is case-aware . 说VB.Net不区分大小写是不正确的,除非你也指出它是区分大小写的 When you declare an variable identifier, the IDE will take note of what case you used and auto-correct other uses to match that case. 当您声明 变量 标识符时,IDE将记录您使用的是什么情况并自动更正其他用途以匹配该情况。 You can use this feature to help spot typos or places where the IDE might be confused about a variable or type. 您可以使用此功能来帮助发现拼写错误或IDE可能与变量或类型混淆的位置。 I've actually come to prefer this to real case-sensitive schemes. 我实际上更喜欢这个实际的区分大小写的方案。

VB.Net imports namespaces differently. VB.Net以不同方式导入名称空间。 If you want to use the File class, you can just say IO.File without needing to import System.IO at the top. 如果要使用File类,只需说IO.File而无需在顶部导入System.IO The feature especially comes in handy when learning a new API with a few nested namespace layers, because you can import a top-level section of API, type the next namespace name, and you'll be prompted with a list of classes in that namespace. 在学习具有一些嵌套命名空间层的新API时,该功能特别有用,因为您可以导入API的顶级部分,键入下一个命名空间名称,并且系统会提示您输入该命名空间中的类列表。 It's hard to explain here, but if you look for it and start using it, you'll really miss it when going back to C#. 这里很难解释,但是如果你寻找它并开始使用它,你回到C#时会非常想念它。 The main thing is that, for me at least, it really breaks my flow to need to jump to the top of the file to add yet another using directive for a namespace I may only use once or twice. 最重要的是,至少对我来说,它真的打破了我的流程需要跳转到文件的顶部为命名空间添加另一个using指令我可能只使用一次或两次。 In VB, that interruption is much less common. 在VB中,这种中断不太常见。

VB.Net does background compilation. VB.Net进行后台编译。 The moment your cursor leaves a line, you know whether or not that line compiles. 光标离开一行时,您知道该行是否编译。 This somewhat makes up for not highlighting class names, because part of why that's useful in C# is so you know that you typed it correctly. 这有点弥补了不突出显示类名,因为在C#中有用的部分原因是你知道你正确输入了它。 VB.Net gives you even more confidence in this regard. VB.Net让您在这方面更有信心。

I have done the same thing in the past. 我过去做过同样的事情。 I'm starting to move away from it though because Visual Studio will occasionally get confused when it auto formats the code and changes the casing on my static method calls to lower case. 我开始摆脱它,因为Visual Studio在自动格式化代码时会偶尔会感到困惑,并且会在我的静态方法调用小写时更改外壳。 That is even more annoying than not being able to differentiate the variable and class names by case only. 这比仅仅通过大小写来区分变量和类名更令人讨厌。 But, purely from technical perspective it should not cause any issues. 但是,纯粹从技术角度来看,它不应该引起任何问题。

I'm going to differ with the rest of the answers here... I don't think there is any problem with doing this. 我将在这里与其他答案不同......我认为这样做没有任何问题。 I do it regularly, and have absolutely 0 problems resulting from it. 我经常这样做,并且完全没有问题。

If you use lowercase for the variable name you can easily differentiate the variable from the type, and the compiler will not confuse the two identifiers. 如果对变量名使用小写,则可以轻松地将变量与类型区分开来,编译器不会混淆这两个标识符。

If you delete the variable declaration, the compiler will think other references to this variable now refer to the type, but it's not really a problem because those will be tagged as errors. 如果删除变量声明,编译器会认为对此变量的其他引用现在引用了类型,但它并不是真正的问题,因为它们将被标记为错误。

As Moayad notes, the compiler can tell the difference--but it's bad practice that can lead to maintenance issues and other side effects. 正如Moayad所指出的那样,编译器可以说明差异 - 但这可能会导致维护问题和其他副作用。

A better practice all-around is to try to name the variable in the context they're being used, rather than just the type name. 全面的更好的做法是尝试在它们被使用的上下文中命名变量,而不仅仅是类型名称。 This leads to self-documenting code and requires fewer comments (comments are greatly abused as an excuse to write dense code). 这导致自我记录代码并且需要更少的注释(注释被滥用作为编写密集代码的借口)。

It's only safe as long as the compiler can always tell whether Foo means the class or the variable, and eventually you'll hit a case where it can't. 只要编译器总是可以判断Foo是表示类还是变量,它是唯一安全的,并且最终你会遇到它不能的情况。 Eric Lippert discusses the sort of thing that can go wrong on his blog . Eric Lippert 在他的博客上讨论了可能出错的事情。

I use this convention all the time, and it's never been a problem. 我一直使用这个约定,这从来都不是问题。 The most natural name for a variable is often the class name, and therefore that's what you should call it (Best name for an arbitrary Line? line.). 变量最自然的名称通常是类名,因此你应该称之为它(任意Line?行的最佳名称)。

The only downside is when some tool interprets the context incorrectly. 唯一的缺点是某些工具错误地解释了上下文。 For example, visual studio 2010 beta 1 sometimes uses the class highlight on variables named the same as the class. 例如,visual studio 2010 beta 1有时会在名为与类相同的变量上使用类突出显示。 That's a bit annoying. 这有点烦人。

Context sensitivity is much closer to how I think than case sensitivity. 上下文敏感度更接近我的想法而不是区分大小写。

VB.NET isn't case sensitive! VB.NET不区分大小写! This equates to: 这相当于:

Foo Foo = new Foo(); // C#

As a standard in our team environment we would use: 作为我们团队环境中的标准,我们将使用:

Dim oFoo as New Foo 'VB.NET

Well, this isn't the final answer, and I don't think there is a definitive one, but the general opinion seems to be that it's not a good idea to use this naming convention! 嗯,这不是最终的答案,我认为没有确定的答案,但一般意见似乎是使用这个命名约定不是一个好主意! There must be one true way to write nice VB.NET variable names though, and I don't like any of the alternatives... 必须有一种真正的方法来编写漂亮的VB.NET变量名称,而且我不喜欢任何替代方案......

Here are links to the official Microsoft guidelines for anyone who's interested, although they don't seem to cover this particular question (please correct me if I've missed it). 以下是任何感兴趣的人的微软官方指南的链接,虽然他们似乎没有涵盖这个特定的问题(如果我错过了,请纠正我)。

Visual Basic Naming Conventions: http://msdn.microsoft.com/en-us/library/0b283bse.aspx Visual Basic命名约定: http//msdn.microsoft.com/en-us/library/0b283bse.aspx

Declared Element Names: http://msdn.microsoft.com/en-us/library/81ed9a62.aspx 声明的元素名称: http//msdn.microsoft.com/en-us/library/81ed9a62.aspx

Cheers all! 干得好!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM