Solved

VB.net v C# Namespace

Posted on 2013-12-14
7
368 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 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:murbro
ID: 39718604
Thanks
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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 introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

772 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