Are there any problems using Dim foo As Foo in VB.NET?

In a recent VB.NET project, I adopted the naming conventions that I use for use in C #. Namely, often calling a variable the same name as the class it refers to, only with a different case, for example.

Foo foo = new Foo(); // C# Dim foo As New Foo() ' VB.NET 

I find that this is often the best way to write code, especially for small methods. This coding style obviously works fine in C #, being case sensitive, and due to the syntax highlighting provided by Visual Studio, it is very easy to see that the class name and variable name are different.

However, to my surprise, this also worked fine in almost 100% of cases * in VB.NET. The only problem was that instead the variable name received several identifiers. Namely, it can be used to call instance methods and general (static) methods of the Foo class. It really didn't cause any problems, but it meant that Intellisense would provide a list containing both static and instance methods after you click ".". after the variable name.

Again, to my surprise, I discovered that this did not actually lead to any confusion in my project, and it was very successful! However, I was the only person who worked 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 problem I encountered was that sometimes the rename refactoring tool got confused, i.e. if you rename a class, he would have renamed the variables with the same name as the class, in their Declaration line ( Dim foo As... ), but no other references to this variable, causing the compiler issues (duh). They are always easy to fix.

Another minor annoyance is that the VB.NET syntax syntax does not allocate class names other than variable names, which makes it not entirely pleasant, as when using it in C #. I still found the code very readable, though.

Has anyone else tried to resolve this in a command environment? Are there other possible problems with this naming convention in VB.NET?

+4
source share
9 answers

I will differ in the rest of the answers here ... I do not think that this can be encountered. I do this regularly and it has absolutely 0 problems.

If you use lower case for the variable name, you can easily distinguish the variable from this type, and the compiler will not confuse the two identifiers.

If you delete the variable declaration, the compiler will think that other references to this variable are now of type, but this is not a problem, because they will be marked as errors.

+2
source

Although VB is case insensitive, the compiler is smart enough not to be confused between an instance object and a class.

However, of course, it is very dangerous and incorrect to use the same name in a case-insensitive language! Especially if other programmers are working on this project.

+7
source

I need to move back and forth between VB and C #, and we are looking at this bad practice. We also do not like to allow variable names in C # to differ from their type only in each case. Instead, we use the prefix _ or give it a more meaningful name.

Whenever you start a new language, it is inevitable, you will notice a bunch of things that are different from each other and skip the old way of doing something. Often this is due to the fact that you are not initially familiar with various functions in another language that affect the same problem. Since you're new to VB, here are a few notes to help you with this:

It is not 100% correct to say that VB.Net is not case sensitive unless you also think that it depends on the case. When you declare a variable id , the IDE takes note of which case you used, and automatically adjusts other uses to that case. You can use this function to help identify typos or places where the IDE might be confused about a variable or type. I really preferred this for real case-sensitive circuits.

VB.Net imports namespaces in different ways. If you want to use the File class, you can simply say IO.File without requiring a System.IO import at the top. This feature is especially useful when learning a new API with several nested namespace layers, since you can import the top-level section of the API, enter the following namespace name, and you will be asked to list the classes in this namespace, It’s hard to explain here, but if you are looking for start using it, you really miss it by returning to C #. The main thing is that for me, at least, it really breaks my stream to go to the top of the file to add another directive for the namespace, which I can use only once or twice. In VB, this interrupt is much less common.

VB.Net is building the background. When your cursor leaves the line, you know the compilation of this line. This compensates somewhat for not highlighting class names, because part of why this is useful in C #, so you know that you typed it correctly. VB.Net gives you even more confidence in this regard.

+4
source

I have done the same thing in the past. I am starting to move away from it, although Visual Studio sometimes gets confused when it automatically formats the code and changes the cover art for my calls to the static method to lowercase. This is even more annoying than the inability to differentiate the names of variables and classes only on a case-by-case basis. But, from a technical point of view, this should not cause any problems.

+2
source

As Moyad notes, the compiler can tell the difference - but this is bad practice that can lead to maintenance problems and other side effects.

Best practice is to try to name this variable in the context in which they are used, and not just the type name. This leads to self-documenting code and requires fewer comments (comments are heavily abused as an excuse for writing tight code).

+1
source

This is safe as long as the compiler can always tell if Foo means a class or variable, and you end up in a case where it fails. Eric Lippert discusses what might go wrong on his blog .

+1
source

I use this convention all the time and it has never been a problem. The most natural variable name is the name of the class and, therefore, what you should call (The best name for an arbitrary line is Line.).

The only drawback is when a tool misinterprets the context. For example, visual studio 2010 beta 1 sometimes uses class allocation for variables with a name similar to the class. This is a bit annoying.

Context sensitivity is much closer to how I think than case sensitivity.

+1
source

VB.NET is not case sensitive! It corresponds:

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

As standard in our team environment, we will use:

 Dim oFoo as New Foo 'VB.NET 
0
source

Well, this is not the final answer, and I don’t think there is a final answer, but the general opinion seems to be that it is not recommended to use this naming convention! There must be one surefire way to write good VB.NET variable names, although I don't like any alternative ...

Here are links to official Microsoft manuals for those who are interested, although they don't seem to address this specific issue (please correct me if I missed this).

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

Declared element names: http://msdn.microsoft.com/en-us/library/81ed9a62.aspx

Greetings to all!

0
source

All Articles