Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VB.net v C# Namespace

Posted on 2013-12-14
7
Medium Priority
?
379 Views
Last Modified: 2013-12-14
Hi. I am converting code from C# to VB.net. I notice that in a class module C# always has
"Namespace SAP_Connect_CSharp", while VB.net doesn't. What is this and does VB.net need it?



Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports SAP.Middleware.Connector

Namespace SAP_Connect_CSharp
      Class Customers
            Protected CustomerNo As String
            Protected CustomerName As String
0
Comment
Question by:Murray Brown
7 Comments
 
LVL 21

Assisted Solution

by:Dale Burrell
Dale Burrell earned 1000 total points
ID: 39718444
Very simply, using a namespace allows you to group you code in a logical manner, within a given namespace all names must be unique, but create a new namespace and you can reuse the names again within the namespace.

There is more to it than that, but that gives you an idea - its a fundamental programming concept.

Couple of links which go into more detail.

http://msdn.microsoft.com/en-us/library/ms973231.aspx

http://msdn.microsoft.com/en-us/library/zt9tafza.aspx
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1000 total points
ID: 39718451
while VB.net doesn't.
There is a namespace, you just don't always see it in VB. VB will set a default namespace that is the same name as your project. Your modules would fall under this namespace. You can view your class in the Object Browser to confirm.
0
 

Author Closing Comment

by:Murray Brown
ID: 39718604
Thanks
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:Angelp1ay
ID: 39718608
I'm not sure that's quite true kaufmed. My understanding is:
- VB.NET has a root namespace defined on the project which is *added* to anything you put in the class
- C# has a default namespace defined on the project which is written into every class and can be replaced
- Both are initially the project name

Example - if you want your class to be in the namespace "MyProject.DataAccess":
--- VB.NET ---
Project: Root Namespace = MyProject
Class: Namespace DataAccess

--- C#.NET ---
Project: Default Namespace = MyProject
Class: Namespace MyProject.DataAccess

Open in new window

0
 
LVL 40
ID: 39718916
The basic (default) namespace for both language is set to the project's name when you create a new project, but is really defined in the project's Properties window, in the Application tab, and can be changed if needed.

In C#, this default namespace is used by the editor, when you create a new class, while in VB it is used by the compiler, every time you compile.

In C#, you need to specify the namespace at the beginning of each class, so the editor does it for you. If you ever change the namespace in the project's properties later, classes that where already created will keep their old namespace, but new classes added to the project will get the new default.

In VB, you do not have to specify a namespace for each glass, because the compiler will do it for you later. If you change the default, contrary to C#, the namespace is changed everywhere.

Although the notion of namespace is the same in both languages, the way it is implemented in the Visual Studio interface is different.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39719045
In C#, you need to specify the namespace at the beginning of each class,
This is incorrect. You are not forced to declare a namespace in your class, though the editor does do it for you. Neglecting to specify a namespace in C# means that your class will fall under the global namespace.

It is a best practice to declare a namespace, however.
0
 
LVL 40
ID: 39719156
Right kaumed. This was not stated clearly.

I should have said "need to specify the default namespace if you need the class to fall under it".
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Integration Management Part 2
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question