ProblemYou would like to add some general methods and fields that are accessible to your entire application. SolutionAdd a code modulea construct that is similar to a class, but uses the Module key-word instead of Classto your application. DiscussionVisual Basic includes three major code and value containers: classes, structures, and modules. All three types are based on the core definition of a class, but there are times when you'll want to choose one over another. Modules are useful for storing functions, subroutines, constants, and variable fields that are considered "global" to your entire application. In pre-.NET versions of Visual Basic, most nonform-specific code was stored in a similar "module file" (with a " .bas" file extension). Modules in .NET provide some of that same functionality but in an object-oriented context. If you've already created a new project or opened an existing project in Visual Studio, you can add a new module through the Project Add Module menu command. The Add New Item dialog (Figure 3-1) should already have the Module template selected. Simply give it a useful name in the Name field, then click the Add button. Figure 3-1. Visual Studio's Add New Item dialogVisual Studio presents you with the code for this new template: Module Module1 End Module You can start adding members to the module immediately. Supported members include Sub procedures, functions, properties, events, delegates, classes, structures, and enumerations. Before coding each member, decide the access you want to grant and prefix the definition with the appropriate access keyword ( Public, Shared, or Friend). For instance, the following block of code adds a function to the module Module1 and assigns the function Public access: Module Module1 Public Function DoubleIt(ByVal origValue As Integer) _ As Integer Return origValue * 2 End Function End Module Modules specify their own access levels, using the Public or Friend keywords; the default is Friend. All members of a module act as if they are marked with the Shared keyword. That is, you can use any member of a module without creating an instance of the module itself. And that's a good thing because Visual Basic will not allow you to create an instance of a module. You aren't required to create separate source- code files for new modules (or for classes or structures, which are discussed in later recipes), although you should. Having a one-to-one correspondence between modules (or classes or structures) and source-code files makes things easier to manage. Still, you may need to double up constructs in a single source-code file. If you already have a file with a class defined, you can include a module definition in the same file, outside the class: Class SomeClass ' ----- Class members go here. End Class Module SomeModule ' ----- Module members go here. End Module If you try to do this in a form class file for a desktop application project, the Visual Studio Form Designer looks only at the first class in the file. If you insert a module (or a structure or another class) before the form-derived class in the file, Visual Studio can't display the form. All members of a module are shared and can be used immediately throughout the application. You can limit a member to just the code within the module by using the Private access-level keyword with that member: Module Module1 Private Sub InModuleUseOnly() End Sub End Module This is commonly done with so-called helper methods that can be accessed only by other, more prominent methods in the same module. See AlsoRecipes 3.2 and 3.3 introduce classes and structures, the two other major type constructs in Visual Basic. |