Chapter 3: Program and Module Structure


A Visual Basic solution contains one or more related projects. A project contains files related to some topic. Usually, a project produces some kind compiled output (such as an executable program, class library, control library, and so forth). The project includes all the files related to the output, including source code files, resource files, documentation files, and whatever other kinds of files you decide to add to it.

This chapter describes the basic structure of a Visual Basic project. It explains the functions of some of the most common files and tells how you can use them to manage your applications.

This chapter also explains the basic structure of source code files. It explains regions, namespaces, and modules. It also describes some simple typographic features provided by Visual Basic such as comments, line continuation, and line labels. These features do not execute programming commands themselves, but they are an important part of how you can structure your code.

Hidden Files

Figure 3-1 shows the Solution Explorer window for a solution that contains two projects. The solution named MySolution contains two projects named WindowsApplication1 and WindowsApplication2. Each project contains a My Project item that represents the project’s properties, various files containing project configuration settings, and a form named Form1.

image from book
Figure 3-1: A solution contains one or more projects that contain files.

In WindowsApplication2, the Show Hidden Files button has been clicked (the second button from the left with the box around it) so that you can see all the project’s files. WindowsApplication1 has similar files, but they are hidden by default.

These files are generated by Visual Basic for various purposes. For example, Resources.resx contains resources used by the project and Settings.settings contains project settings.

Tip 

Resources are chunks of data that are distributed with the application but that are not intended to be modified by the program. These might include prompt strings, error message strings, icons, and sound files. For example, resources are commonly used for customizing applications for different languages. You build different resource files for different languages, and the program loads its prompts and error messages from the appropriate resource file. Chapter 27 has more to say about resources.

Settings are values that control the execution of the application. These might include flags telling the program what options to display or how to perform certain tasks. For example, you could build different profiles to provide settings that make the program run in a restricted demo mode or in a fully licensed mode.

The following list describes the files contained in WindowsApplication2 and shown in Figure 3-1. The types of files generated for a project changed a bit between some of the early versions of Visual Studio 2005, so the exact files you see may be different from those shown here. This list should give you an idea of what’s involved in building a project, however.

  • WindowsApplication2 - This folder represents the entire project. You can expand or collapse it to show and hide the project’s details.

  • My Project - This folder represents the project’s assembly information, application-level events, resources, and configuration settings. Double-click the My Project entry to view and edit these values.

  • Application.myapp - This XML file defines application properties (such as whether it’s a single instance program and whether its shutdown mode is AfterMainFormCloses or AfterAllFormsClose).

  • Application.Designer.vb - This file contains code that works with the values defined in Application.myapp.

  • AssemblyInfo.vb - This file contains information about the application’s assembly such as copyright information, company name, trademark information, and assembly version.

  • Resources.resx - This resource file contains project’s resources.

  • Resources.Designer.vb - This file contains Visual Basic code for manipulating resources defined in Resources.resx. For example, if you define a string resource named Greeting in Resources.resx, Visual Basic adds a read-only property to this module so you can read the value of Greeting as shown in the following code.

      MessageBox.Show(My.Resources.Greeting) 

  • Settings.settings - This file contains settings that you can define to control the application.

  • Settings.Designer.vb - This file contains Visual Basic code for manipulating settings defined in Settings.settings, much as Resources.Designer.vb contains code for working with Resources.resx. For example, the following code uses the UserMode setting.

      If My.Settings.UserMode = "Clerk" Then ... 

  • References - This folder lists references to external components such as DLLs and COM components.

  • bin - This folder is used to build the application before it is executed. It contains the compiled .exe file.

  • obj - This folder is used to build the application before it is executed.

  • ApplicationEvents.vb - This code file contains application-level event handlers for the MyApplication object. For example, it contains the application’s Startup, Shutdown, and NetworkAvailabilityChanged event handlers.

  • Form1.vb - This is a form file. It contains the code you write for the form, its controls, their event handlers, and so forth.

  • Form1.Designer.vb - This file contains designer-generated Visual Basic code that builds the form. It initializes the form when it is created, adds the controls you placed on the form, and defines variables with the WithEvents keyword for the controls so that you can easily catch their events.

Some projects may have other hidden files. For example, when you add controls to a form, the designer adds a resource file to the form to hold any resources needed by the controls.

Normally, you do not need to work directly with the hidden files. You can use other tools to modify them indirectly instead. For example, the files Resources.Designer.vb, Settings.Designer.vb, and Form1.Designer.vb are automatically generated when you modify their corresponding source files Resources.resx, Settings.settings, and Form1.vb.

You don’t even need to work with all of these source files directly. For example, if you double-click the My Project item in Solution Explorer, the property pages shown in Figure 3-2 appear. The Application tab shown in this figure lets you set high-level application settings. The View Application Events button at the bottom of the figure lets you edit the application-level events stored in ApplicationEvents.vb.

image from book
Figure 3-2: These property pages let you define project’s resources, settings, and general configuration.

The Resources tab shown in Figure 3-2 lets you view, add, and remove project references. As you can probably guess, the Resources and Settings tabs let you edit the project’s resources and settings.

A particularly important section hidden away in these tabs is the assembly information. When you click the Assembly Information button shown in Figure 3-2, the dialog box shown in Figure 3-3 appears.

image from book
Figure 3-3: The Assembly Information dialog box lets you define basic project information such astitle, copyright, and version number.

Many of the items in this dialog box, such as the application’s title and description, are self-explanatory. They are simply strings that the assembly carries around for identification. The assembly and file versions are used by the Visual Studio runtime to verify compatibility between an application’s components. The GUID (globally unique identifier) uniquely identifies the assembly and is generated by Visual Studio. The Make assembly COM-Visible check box lets you determine whether the assembly should make types defined in the assembly visible to COM applications. For more information on this dialog box, see msdn2.microsoft.com/en-gb/library/1h52t681(VS.80).aspx.

An assembly is the fundamental unit of deployment and version control in Visual Studio .NET. An assembly can contain an executable application, a DLL, or control library. Usually a project is contained in a single assembly.

The Assembly Information dialog box lets you define information that should be associated with the assembly, including the assembly’s company name, description, copyright, trademark, name, product name, title, and version (which includes major, minor, revision, and build values).

The My.Application.AssemblyInfo namespace provides easy access to these values at runtime. The following code shows how a program can display this information in a series of labels when it starts.

  Private Sub Form1_Load(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles MyBase.Load     lblCompanyName.Text = My.Application.Info.CompanyName     lblDescription.Text = My.Application.Info.Description     lblCopyright.Text = My.Application.Info.Copyright     lblTrademark.Text = My.Application.Info.Trademark     lblDirectoryPath.Text = My.Application.Info.DirectoryPath     lblProductName.Text = My.Application.Info.ProductName     lblTitle.Text = My.Application.Info.Title     lblVersion.Text = My.Application.Info.Version.ToString End Sub 




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net