0

I want to define a unified namespace for all my classes and put them all in it. I did it with

Namespace
...
End Namespace

But now I can not import it in other projects, also I can not import it in another file in my main project (That file is not in my namespace it is just UI).

Thank you in andvance !

6
  • Did you add the right reference to the project? Commented Feb 7, 2019 at 15:31
  • It's not going to be in the references. You simply need to have the right Imports at the top of your file. Commented Feb 7, 2019 at 15:33
  • A reference is a dll or an other project. A namespace is visible from the list of all references. Commented Feb 7, 2019 at 15:33
  • What is right import ? i wrote Import namespace name , but did not recognized Commented Feb 7, 2019 at 15:35
  • Are you trying to use these classes in the same project or a different project? Commented Feb 7, 2019 at 15:38

1 Answer 1

3

Your project has a root namespace defined in the project properties. Let's say this root namespace is MyApp.MyProject.

Then inside this project, you have a class defined like this:

Namespace MyNamespace

    Public Class MyClass...

End Namespace

If you want to use MyClass inside another class, you would just add the following Imports statement at the top of the file:

Imports MyApp.MyProject.MyNamespace

You could also define a Namespace globally outside of the root namespace of your project with Global like so:

Namespace Global.MyNamespace
    ...
End Namespace

More information can be found in the .NET documentation

Sign up to request clarification or add additional context in comments.

6 Comments

It's worth mentioning that, if you want to define or specify a namespace which is not under the root namespace, you still can do that by prefixing it with Global (e.g. Namespace Global.MyOtherNamespace)
@StevenDoggart Actually it is what I need , Thank you !
@FASW Using global namespace should be discouraged. There is little need for it. The more you put there, the more it becomes muddied. It's best practice to avoid naming collisions and have structured namespaces.
@FalcoGer: Yeah I don't get why you would need a global namespace either. Personally I never used it.
On VB Projects, I generally clear the root namespace and just use the Namespace statements. This makes it similar to the way C# does it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.