Solved

VB.net v C# Namespace

Posted on 2013-12-14
7
374 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 75

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:Murray Brown
ID: 39718604
Thanks
0
Industry Leaders: 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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

636 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