Solved

VB.net v C# Namespace

Posted on 2013-12-14
7
364 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:murbro
7 Comments
 
LVL 21

Assisted Solution

by:Dale Burrell
Dale Burrell earned 250 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 74

Accepted Solution

by:
käµfm³d   👽 earned 250 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:murbro
ID: 39718604
Thanks
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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 74

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now