views:

629

answers:

3

How do you define your UserControls as being in a namespace below the project namespace, ie. [RootNameSpace].[SubSectionOfProgram].Controls?

Edit due to camainc's answer: I also have a constraint that I have to have all the code in a single project.

Edit to finalise question: As I suspected it isn't possible to do what I required so camainc's answer is the nearest solution.

+1  A: 

I'm not sure if this is what you are asking, but this is how we do it.

We namespace all of our projects in a consistent manner, user controls are no different. We also namespace using the project settings window, although you could do it through a combination of the project window and in code.

Each solution gets a namespace like this:

[CompanyName].[SolutionName].[ProjectName]

So, our user controls are normally in a project called "Controls," which would have a namespace of:

OurCompany.ThisSolution.Controls

If we have controls that might span several different solutions, we just namespace it like so:

OurCompany.Common.Controls

Then, in our code we will import the library, or add the project to the solution.

Imports OurCompany
Imports OurCompany.Common
Imports OurCompany.Common.Controls

We also name the folders where the projects live the same as the namespace, down to but not including the company name (all solutions are assumed to be in the company namespace):

\Projects
\Projects\MySolution
\Projects\MySolution\Controls

-- or --

\Projects\
\Projects\Common
\Projects\Common\Assemblies
\Projects\Common\Controls

etc.

Hope that helps...

camainc
Good solution to the original question so I have marked you up. However, it does not solve my current problem so I have added some further clarification to the question.
Garry Shutler
A: 

Do you mean you want to be able to access user controls at runtime (in code) via

[ProjectNamespace].[YourSpecialNamespace].Controls

rather than the default of

[ProjectNamespace].Controls

? Because I don't believe that is possible. If I'm not mistaken, the Controls collection of your project/app is built-in by the framework - you can't change it. You can, as camainc noted, use the project settings window (or code) to place the controls themselves in a specific namespace thusly:

Namespace [YourSpecialNamespace]

Public Class Form1

[...]

End Class

End Namespace

Of course, thinking about it some more, I suppose you could design and build your own Controls collection in your namespace - perhaps as a wrapper for the built-in one...

Keithius
A: 

If you don't want the controls to be in a separate project, you can just add the Namespace keyword to the top of the code file. For example, I've done something like this in several projects:

Imports System.ComponentModel

Namespace Controls
   Friend Class FloatingSearchForm

      'Your code goes here...

   End Class
End Namespace

You will not be able to specify that the controls are in a different root namespace than that specified for the project they are a part of. VB will simply append whatever you specify for the namespace to the namespace specified in the project properties window. So, if your entire project is "AcmeCorporation.WidgetProgram" and you add "Namespace Controls" to the top of a control file, the control will be in the namespace "AcmeCorporation.WidgetProgram.Controls". It is not possible to make the control appear in the "AcmeCorporation.SomeOtherProgram.Controls" namespace.

Also note that if you are using the designer to edit your controls, you need to add the Namespace keyword to the hidden partial class created by the designer. Click the "Show All Files" button in the solution explorer, then click the expand arrow next to your control. You should see a "*.Designer.vb" file listed. Add the Namespace to that file as well. The designer will respect this modification, and your project should now compile without error. Obviously, the namespace specified in the designer partial class must be the same one as that specified in your class file! For the above example:

Namespace Controls
   <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
   Partial Class FloatingSearchForm

      'Designer generated code

   End Class
End Namespace
Cody Gray