views:

239

answers:

3

I am converting projects from C# to Visual Basic, and the namespaces in VB.NET behave in a weird way. There is some kind of hidden default namespace, and it's annoying. I want it to behave identical to C#, which works as expected - things go into the namespaces you create for them.

I've been getting around it usually with say

using MyClassLibrary;

in C#, and in VB

Imports MyClassLibrary
Imports MyClassLibrary.MyClassLibrary

but it would be nice to have the functionality the same, and also logical.

The other bigger problem is, I have a .tt file, and the C# project generates the code in a different namespace to the VB one.

Is there some solution to make both behave identically with regards to namespaces?

A: 

Check the project's properties, there is a default namespace option that may be set in the VB project but not the C# one. The namespaces should behave the same across the languages, with the exception of VB's "My" namespace.

pezi_pink_squirrel
I'm not seeing a default namespace option anywhere...
SLC
I believe it may be called "Root Namespace" in VB, they are both in the Application tab of the project's properties, in the top right.
pezi_pink_squirrel
I'm still getting problems with my .tt file autogenerating code with a different namespace to the C# solution... any ideas on that? The C# solution generates the namespace MyClassLibrary which is correct, but the VB solution generates a different namespace that means stuff using my class library has to import MyClassLibrary and MyClassLibrary.MyOtherNamespace
SLC
incorrect, c# default namespace and vb root namespace do not behave the same way. The root namespace in vb is prepended to all namespaces in the project, in c# the default namespace is put in the source file for you. So in vb.net if the root namespace is MyRootNamespace and you have a source file with the namespace MySourceNamespace then the actual namespace you have to use to access anything in MySourceNamespace is MyRootNamespace.MySourceNamespace. VB will automatically import the root namespace for you in that project.
David
IMHO a best practice here is to remove the root namespace first thing from any VB.Net project as soon as you create it and use the namespace in the source files.
David
If that's correct, if you create a solution with project A and project B, to access the resources of project B from project A, you would have to do using B.B which is not the case
SLC
Only if you put a namespace of B within the source files for B, in which case you would have to access them as B.B
David
After much investigation, I have decided it's a really bad idea to remove the default namespace from VB, as the behaviour doesn't end up the same. While it probably works for standard classes, when it comes to resources, they often end up in the wrong namespace with no way to change it. For example, in C#, in a project called 'DOG', adding an .edmx with its properties set to 'CAT' as the namespace, the codebehind file will remain as 'DOG' in C#, but in VB, will be 'CAT'. If you change it to 'DOG' manually, it creates problems elsewhere where some bits of code are looking for 'CAT' still.
SLC
Overall it created so many problems that it was easier just to fudge it by importing whatever was required to make the visual basic version work. VB is just too quirky. My explanation isn't great, but it was a headache to get it working and it ended up messier than leaving the namespace intact.
SLC
+2  A: 

Check Root namespace in VB project options. Just clear it.

This is not the same as Default namespace in C# projects. If you change Default namespace in C# projects, existing files don't change. If you however change Root namespace in VB project, this will affect all existing members.

Peter Macej
to clarify; this is in fact the same though, correct? It's just one goes off and refactors existing files, whilst the other doesn't?
pezi_pink_squirrel
As the namespace is set by default, I can't see how it could be the same.
SLC
@pezi: no, it doesn’t refactor files … but when you compile the project (as opposed to individual files via the command line) the compiler prepends the root namespace to all namespaces (even if the source file does not specify any namespace).
Konrad Rudolph
@Konrad - Ahh I see, thanks for the explanation, I was not aware of that.
pezi_pink_squirrel
@Konrad - How can that be correct? By default, in a blank project, the Default Namespace is set to MyProjectName. If you create a new class, it starts with Namespace MyProjectName. So the Default Namespace must be ignored. I imagine Default Namespace is used when you neglect to specify one. It would be strange for it to prepend it to everything.
SLC
I have an .edmx file with a codebehind. The C# version generates the codebehind with MyClassName which is good - even though its different to the edmx file namespace of MyEdmxFile. The VB version however generates the codebehind with MyEdmxFile which is wrong and annoying because it requires an import. I'll just chalk it up to VB sucking
SLC
@SLC: Aren’t you confusing the **C# default namespace** with the **VB root namespace** here? In VB, when you start a new class file, no namespace is entered, *exactly because* the root namespace is prepended.
Konrad Rudolph
A: 

I can only conclude that while the above answer of removing the root namespace from VB.NET appears to work, and probably does work for some solutions to imitate the way C# handles namespaces, that other files such as entity designer files and autogenerated code & template namespaces behave differently and you cannot change their behaviour without hacking them apart. After doing one of my conversions in this way, I had such trouble with some of the files defining their own namespaces in a different way to C# that I decided it was better to live with the built in quirks of visual basic, rather than try to bypass them.

SLC