VB.NET Namespace

Filed under C++, Visual Basic
Tagged as , , , , ,

The following Q&A items from stackoverflow found 21st Aug 2011 relating to Visual Basic .NET Namespaces (with responses/answers).  Please see the Q&A item on stackoverflow for more details and answers, and if you have questions or comments.

Extending a class by namespace

I have a class within a library, with no root namespace, firstone.dll:

namespace first
    public partial class one
        public sub fun()

        end sub
    end class
end namespace

My second library, with no root namespace, firstonetwo.dll, references firstone.dll:

namespace first.one
   public partial class two
       public sub testfun()
            first.one.fun() 'not recognized'
       end sub
   end class
end namespace


namespace first
   public partial class one
        public partial class two
            public sub testfun()
                 first.one.fun() 'also not recognized'
            end sub
        end class
   end class
end namespace

Is there a way to extend the class in a separate dll and still have access to the original class? I don’t want to inherit the class just extend it.

Chris Haas: The problem is that you have a namespace and a class with the same name and that causes a lot of confusion. Your first example is a class called one in the first namespace and your second example is a class called two in the first.one namespace. Just because they can be written the same, there’s big difference between the two.

Do not name a class the same as its namespace, Part One:


[…]VB is having a problem walking the tree and determining which one to look at.


Choose the full namespace/class/function in VB.NET

-edit- nevermind i was lucky and didnt need to change much and have this test working.

I am doing a quick test and i need to choose the full path to a function.

in C# i can write

namespace ABC { class DEF { static string Test() { return "A test"; } } }

and can access it with ABC.DEF.Test() i tried

Namespace ABC
    Partial Public Class DEF
        Public Function Test(ByVal v As String) As String
            Return "A Test"
        End Function
    End Class
End Namespace

and i got VBTest.ABC.DEF.Test(). How do i get rid of VBTest which is the name of my project?

Oded: Look in the project properties under the Application section – it will have a Root Namespace entry that you can change (remove VBTest from it).


Do you use the ‘My’ namespace in VB.NET?

VB.NET has the “my” namespace, but how many VB.NET developers actually use it?

  • if you don’t, why?
  • if you are using it, why?

I’m considering building a framework for VB.NET, and using the My namespace to plug it into VB seems like a reasonable idea. Is it?

Kyralessa: The purpose of My, as I understand it, is to be an easy shortcut to certain API tasks that are common but hard-to-find or hard-to-use. You probably shouldn’t completely subsume your framework under My. (For one thing, C# people using your framework may get grouchy.)

Instead, you should design it as a normal framework. When you’re finished, make a list of some common tasks that people might want to use your framework for. See whether any of those could be useful to have under My, especially where there are classes or methods that can be used in a number of ways, but they have one or two really common usages that can be abbreviated with My.

This article shows how to extend My, and it has a section at the end that describes a few design guidelines to follow: Simplify Common Tasks by Customizing the My Namespace

As to your main question, when coding in VB .NET, I use My as often as I can. It reduces a number of operations to one line of code.


namespace and use keyword [closed]

why use of namespace and use keyword and declaration of namespace — using System.Data, using tells compiler to use certain name space otherwise it would be other kind of declaration like public variable for instance. – eugeneK

Fredrik Mörk: Namespaces are used for organizing the types in the code in a logical manner. Imagine the Base Class Library itself, with its many thousands of types, without namespaces. It is simply a tool for making code more structured, much like the ability to create directories in the file system helps you organize your files.

Now, since the full name of a type is the namespace and type name, these may become fairly long. In order to make this easier on you as a developer, you can tell the compiler that you are using types from certain namespaces in a code file with using (or Imports in VB.NET) directives, and then you don’t need to use the full name in that file. It simply tells the compiler where to go look for any type that is not entered using the full name in the code.


Possible to override VB.NET root namespace?

VB.NET automatically prefixes the root namespace set in the project properties to the namespace of each class. This is different from C#, where the full namespace must be declared each time.  Is it possible to override this behaviour, creating a namespace outside of the root namespace?

RobS: If I understand you correctly you just need to set a blank namespace in the project properties dialog and then set namespaces within each source file using Begin/End Namespace commands.


Namespace being automatically imported

I know I am overlooking something really simple here.

I am using Visual Studio 2008 and created an ASP.NET 3.5 project and solution. I added another project to my solution, a class library. I added a reference to my class library. Now when I click on the properties of my class library, the Default Namespace is set to “HRCommon”, which is correct.

Now for some reason, from my ASP.NET application, it’s automatically importing the HRCommon namespace. So when I want to reference a class out of my class library, I just need to type the classname. I want to have to specify the whole name, like HRCommon.<ClassName> instead of just <ClassName>.

Anyone tell me what I’m overlooking here? The language is C#, btw.


SLaks: What namespace is your ASP .Net code in?

It’s probably inside of HRCommon.

If you write code in a nested namespace, it will automatically import all parent namespaces (eg, code in System.Windows.Forms doesn’t need to import System.Windows and System)

To prevent this, move either the library or the ASP .Net project to a different namespace.

Alternatively, you might not have namespace statements in the library.

The Default Namespace option in VS is only used to generate the namespace block in new C# source. EDIT: To clarify, the Default Namespace setting is not used by the compiler at all. It’s only used by the Visual Studio to automatically insert namespace blocks in new source files.

Check the source files in the library and make sure that all of the classes are defined inside of the namespace HRCommon.

For example:

namespace HRCommon {
    public class MyClass {


Visual Basic and a Namespace Issue

I was cleaning up a Visual Basic (.NET 2.0) solution. Splitting it into two projects. 150 classes have the Namespace RebateCalculator at the top of the file. These files are now sitting in Project with the default namespace RebateCalculator. If I were to insert a Class1.cs file and then namespace declaration that all my other files have – then the fully-qualified class name would be RebateCalculator.RebateCalculator.Class1 […]

Chris Dunaway: …  VB.Net has the concept of a “Root Namespace”. When you add a Namespace statement to the code file, the name given is combined with the root namespace to form the full namespace.

So, in VB.Net, if your root namespace is MyRoot and then you create a class and surround with a namespace of MyNamespace, then the full class name is MyRoot.MyNamespace.ClassName. If you don’t use a Namespace statement, then the full class name is MyRoot.ClassName. In other words, the root namespace setting contributes to the name of the namespace.  …

VB.NET Namespace

Information about how C# and VB.NET handle namespaces differently.

Comments are closed.