在VB.NET使用暗淡富有问题由于富? [英] Are there issues using Dim foo As Foo in VB.NET?

查看:116
本文介绍了在VB.NET使用暗淡富有问题由于富?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在最近的VB.NET工程采用了我,我习惯了使用C#中的命名约定。也就是说,经常调用一个变量相同的名称,因为它引用类,只能用不同的情况下,如:

In a recent VB.NET project I adopted the naming conventions I'm used to using in C#. Namely, often calling a variable the same name as the class it references, only with a different case, e.g.

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

Dim foo As New Foo() ' VB.NET

我觉得这往往是写代码,尤其是对小方法最清晰的方式。这种编码风格显然是工作在C#精细,是区分大小写,因为语法高亮显示由Visual Studio提供的,这是很容易地看到,类名和变量名是不同的。

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.

不过,出乎我的意料,这也先后在VB.NET的时间*罚款近100%。唯一的问题是,在变量名,然后出现采取多重身份。即它可以用来通话双方实例方法和Foo类的共享(静态)方法。这并没有真正引起任何问题,虽然,它只是意味着,智能感知将提供包括静态和实例方法,你的变量名后击中后的'。'清单。

However, to my surprise, this also worked fine nearly 100% of the time* in VB.NET. 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. 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 '.' 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, i.e. 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). These were always easy to correct though.

另一个小的烦恼是,VB.NET的语法高亮并不突出的类名称有任何不同的变量名,使得它不太作为很好的在C#中使用时,它作为。我仍然发现代码非常易读虽然。

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#. I still found the code very readable though.

有任何人试图在团队环境中,允许呢?是否有在VB.NET此命名约定的其他潜在的问题?

Has anyone else tried allowing this in a team environment? Are there any other potential issues with this naming convention in VB.NET?

推荐答案

我要与其余相差答案在这里...我不认为这是有这样的问题。我经常这样做,并由此产生绝对0的问题。

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.

这篇关于在VB.NET使用暗淡富有问题由于富?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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