Best practices for Forms Development

Warren

New member
Joined
Apr 16, 2008
Messages
4
Programming Experience
5-10
Hello All,

I need advice on best practices in 2 areas of Windows forms.

In my VS 2008 solution I have a project myprogWin which contains all my rms and UI components. In each user control is it best to use the Namespace keyword. For example should I have "Namesspace myprojWin" as the first line or by having all Windows form components in a seperate project reduce the need for this. What is the best practice in this regard?

2) Is it preferred to qualify your object names or should I add import statements at the top of my classes like:

Imports System
Imports System.Data

or should I do this in the Visual Studio references dialog which seems hidden. What is best practices for this. What are others doing.

Thanks in advance.
 
When you create a project the default namespace for the project is the project name. That means that every type in your project is under the "myprogWin" namespace by default. If you then go and add "Namespace myprogWin" to a code file, any types within that block will be in the "myprogWin.myprogWin" namespace. Obviously NOT what you want.

If you want best practices then you can start by not using names like "myprogWin". Use sensible, readable, descriptive names for your projects and your namespaces. For instance, if you're creating a test project for Windows Forms then a sensible project name would be "Windows Forms Test". The default namespace for that project will then be "Windows_Forms_Test". You would then edit the default namespace in the project properties to "WindowsFormsTest" or "WinFormsTest" or the like.

Now, if you DO want to nest namespaces in a project then there's a proper way to do that too. Let's say that you have a project as above, i.e. with a default namespace of "WindowsFormsTest". You could then right-click the project in the Solution Explorer and select Add -> New Folder. You might call that folder "Forms" and keep all your forms in that folder. Instead of adding a new form to the project you would right-click the folder and add a new form to it. The system will then add the "Namespace Forms" block to the generated code file and the new form class will be a member of the WindowsFormsTest.Forms namespace.

As for qualifying types or importing namespaces, in general it is preferable to import namespaces. If you're using members of a namespace a lot you should add a project-wide import in the project properties, which is where all the default imports are. If you're only using it once or twice then you might choose to add imports directly to the code files that need them. This reduces the risk of name clashes in code files. If you only use a namespace once then you may as well just qualify the type name in code.

By not qualifying type names in code you tend to make your code more readable by virtue of its being less verbose. If you aren't very familiar with a particular namespace though, you may choose not to import it so that you have to qualify its members and therefore familiarise yourself with it.
 
Back
Top