Private Namespace in VB.NET

  • Thread starter Thread starter Joshua Frank
  • Start date Start date
J

Joshua Frank

Hi All,

Is it possible to have a private namespace in VB? When I try it I get
this error:

"Specifiers and attributes are not valid on 'Namespace' statements."

I have a few reasons for wanting this; the most annoying one is that if
you add a web reference to your project, VS creates the proxy objects in
a namespace that you specify. Because namespaces are always public,
this then appears in the object browser as part of the component. But
this is annoying, because I don't want the internal implementation
details (i.e., that my component uses a web service) visible like this.
Namespaces are just an organizational convenience for the programmer;
I can't think of a reason why you shouldn't be able to make one public.
Any ideas on this one? Thanks in advance.

Cheers,
Josh
 
If the namespace is only applicable to that project - then why bother
creating it at all? Why not put the classes you would have put in there,
into one of the public namespaces in the project? As long as the classes
are marked as for the current assembly only, they should not show up
elsewhere.
 
Agreed, but certain tools, like the web service proxy creator I
mentioned, auto generate code and put it in a namespace, so you don't
have the control that you need to do this. But also, once in a while it
would be nice to use a namespace for its intended purpose (i.e.,
organizing code) without publicizing this fact to everyone else.
 
If you create a namespace "mystuff.private" and all the classes in it are
private, will anyone else ever see it?

I don't think they will, because the tools should not list empty namespaces.
Try it and see what happens.
 
Jonathan,
As you may know you can only put private classes inside another Class or
Structure.

The best you can do is make all the classes in "mystuff.private" Friend,
which then requires the namespace be written to the assembly, exposing the
namespace...

Interesting thought though, I had to try it to very verify it.

Hope this helps
Jay
 
Right, you can make all the classes Friend, and they won't show up in
the Object Browser. But the namespace does. The empty namespace
doesn't do much harm, but it's untidy.
 
Joshua said:
Right, you can make all the classes Friend, and they won't show up in
the Object Browser. But the namespace does. The empty namespace
doesn't do much harm, but it's untidy.

Namespaces don't exist in IL, nor do they exist in metadata.
They are just "shortcuts", that save us some time while typing
boring stuff like

ACME.Stuff.Util.Net.MultiSocket s = new
ACME.Stuff.Util.Net.MultiSocket();

Because they don't exist you cannot mark them hidden/private
whatever.

bye
Rob
 
Back
Top