Using Namespace in vb.net is not as good as in c# why?

I was wondering why ms decided to have the way they set up the namespaces differently between the language.

c# when creating a new projects it adds the namespace to the code as well as the property.

in vb it only adds to the property of the project and if you try to add it to the code it will create a double one.

eg

MyCompany.Application.Employee
Namespace MyCompany.Application.Employee

public class stuff
end class
end namespace
calling the above class will create a silly

MyCompany.Application.Employee.MyCompany.Application.Employee

this doensnt happen in c#.
Also if you delete the root namespace in the properties to have total freedom that and add to you classes that it complains and throws errors.

Am i doing something wrong?

thanks to anybody who could give a proper explanation how to use the namespace in vb.net and have total control over it

thanks

[904 byte] By [vbjunkie] at [2008-1-31]
# 1

C# has a Default Namespace property. The default namespace is used by the IDE to set the namespace whenever you add a new file to your project. If you change the default namespace after you have added your files, the namespace will *not* be updated in the existing files. Only files added after the default namespace change will automatically get the new default namespace.

VB has a Root Namespace property. The value of this property is passed to the VB compiler, which will prepend this namespace to all the classes & modules that it compiles. If you add a bunch of VB classes to a project and then change the root namespace, all classes will live in the new root namespace.

Also note that the namespace for an item added to a solution subfolder is different in VB & C#. C# will append the name of the folder to the namespace. VB will not include the name of the containing folder.

In my experience, most projects do have a common "root" namespace. In your example, it would most likely be MyCompany.Application. I would use that as the root namespace for the property, which means that I'd create define the "stuff" class as:

Namespace Employee
Public Class stuff
End Class
End Namespace

If I end up with classes living in multiple top-level namespaces in the same assembly, I usually have a second look to see if it makes sence to split my code into multiple assemblies. If you end up having multiple top-level namespaces in the same VB project, then setting the root namespace to an empty string and add the full namespace is the way to go.

You mention that:
Also if you delete the root namespace in the properties to have total freedom that and add to you classes that it complains and throws errors.

What kind of errors do you get? There have been issues with rename of symbols/auto generated code and empty root namespaces, but I need some more info before I can tell you for sure what's going on here...

If you think that you have found a bug, you can submit it using the Product Feedback center:
http://lab.msdn.microsoft.com/productfeedback/Default.aspx

Best Regards,
Johan Stenberg

JohanStenberg at 2007-9-8 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

First of all I would like to say thank you for you very good response.Fantastic.

I find the use of the namespaces not very flexible in vb.net. I find the c# approach better.


I have the following


Generic Libraries
=================
GB.Library.WinForms
GB.Library.WinControls
GB.Library.DataPortal
===========================================

GB.TravelStudio.Facades.DalTier (base tier)
GB.TravelStudio.Library.Common
GB.TravelStudio.Library.Facades.BizTier (base Tier)


Now i wanted to find a way to group all the business objects under the "BizTier" namespace

So I created a project
GB.TravelStudio.Facades.BizTier.EmployeeBiz
GB.TravelStudio.Facades.BizTier.CustomerBiz
etc etc ...

but then when I went to use the object i would get the following

GB.TravelStudio.Facades.BizTier.CustomerBiz.GB.TravelStudio.Facades.BizTier.CustomerBiz

do you see what i mean?

So the solution to my problem was to have an empty root namespace.This does not happen in c#

THis solved the problem however this caused huge problem when i started to add bitmaps to the resource files
rebuild the solution and says:
"Error 2 Type 'System.Drawing.Bitmap' is not defined."
c:\GB\TravelStudio\TravelStudio.EmployeeBiz\My Project\Resources.Designer.vb 64 61 TravelStudio.EmployeeBiz
"
My conclusion is that in order to achieve a better namespace and have full control of your namespaces the root should be blank
however as soon as you start using the resourceFiles it doenst like it

Am i doing something wrong?

Any more suggestions would be apprecciated


Thanks again

vbjunkie at 2007-9-8 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3

Is it possible for you to stop specifying the namespace in the source code? If so, all the classes that you define in the

GB.TravelStudio.Facades.BizTier.EmployeeBiz

project will live in the GB.TravelStudio.Facades.BizTier.EmployeeBiz namespace and so on. You can still add new child namespaces of course...

I'm not 100% sure if you are sharing source code between your projects, in which case you may have to use an empty root namespace...

Now, if you don't like to use the RootNamespace property, you should certainly be able to use an empty root namespace. This will get you fairly close to the C# namespace behavior - the main difference is that you have to manually specify the namespace for a newly added file (which, at least for me, usually isn't a big issue)

The error you describe when setting the root namespace to an empty string is ususally caused by a missing reference (in this case, you'd need to reference System.Drawing in order to use System.Drawing.Bitmap:s)

If your project has a reference to System.Drawing, then please submit a bug on
http://lab.msdn.microsoft.com/productfeedback/ so we can take a look at it...

It would be helpful to include a copy of the .resx and .resx.designer.vb files in the report.

Best regards,
Johan Stenberg

JohanStenberg at 2007-9-8 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4
Thanks for you help in the matter

It 's so good to see this kind of response

Thanks

vbjunkie at 2007-9-8 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 5
I don′t see at all anything better in the C# approach. It is in all aspects inferior.
I often change namespace names till final release, so you have search and replace in every file.

Besides that, I would always put sub-namespaces in seperate assemblies.

As already said, if wanted, you can simliate the C# way with an empty root namepace. So VB can emulate C#, but C# can′t operate the VB way. So who′s superior ;)

Ramirez at 2007-9-8 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 6

Ramirez,
Don't get me wrong .I am a vb guy ,I just find the way c# handles namespaces personally better.

Thanks for your input

vbjunkie at 2007-9-8 > top of Msdn Tech,Visual Basic,Visual Basic Language...