NibblyPig
NibblyPig

Reputation: 52932

How do you make VB.NET namespaces behave like C#?

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?

Upvotes: 3

Views: 3012

Answers (3)

NibblyPig
NibblyPig

Reputation: 52932

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.

Upvotes: 1

Peter Macej
Peter Macej

Reputation: 5577

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.

Upvotes: 6

pezi_pink_squirrel
pezi_pink_squirrel

Reputation: 774

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.

Upvotes: 0

Related Questions