Anatomy of a VB Project

 

A VB.NET project may have following:

 

·         Forms

·         Classes

·         Modules

 

Each form, class and module has a file associated with it. The chart below shows the various types of files that can be associated with a VB project, along with its file extension, and the equivalent extension from classic VB (VB6).

 

FILE TYPE

VB.NET FILE EXTENSION

CLASSIC VB FILE EXTENSION

Form

.vb

.frm

Module

.vb

.bas

Class

.vb

.cls

Resource file (icons, bitmaps, etc. on a form)

.resx

.frx

Project

.vbproj

.vbp

Solution

.sln

.vbg (VB Project Group)

 

When you create a new standard project (Windows Forms project), VB.NET automatically adds one form to the application.  We can add more forms, standard modules, and classes as needed. 

 

It is worth mentioning here that forms are also classes but have special meaning associated with them in the IDE. The IDE provides a designer to write the code graphically, that is, code is generated automatically for each action taken in the designer.

 

If there is more than one form (or any other code module for that matter), we may need to select a start-up module, typically a form.  This start-up module tells VB.NET which form or module should run first. If a module is set as the start-up object, it must have a public method called Main and Sub Main is specified as the Startup object.

 

Code modules consist of one or more procedures, which are either Subs or Functions.  Procedures are either event procedures or programmer-defined procedures.  Event procedures contain code that you want to execute in response to an event (such as click event of a command button). Programmer-defined procedures are general procedures, called under your control (i.e., not in response a particular external event).  Forms can contain both event and programmer-defined procedures; standard modules can only contain programmer-defined procedures.

 

A Sub or Function procedure can be Public or Private in scope. A Public procedure can be referenced by any other module in the project; a Private procedure can only be referenced within the module in which it is defined. Event procedures, which can only occur in forms, are Private by default. In general, all form procedures should be Private (unless there is a specific reason to make them Public). Public procedures in forms, when called by other modules, must be referred to with "qualifying" syntax: for example, to call Public Sub "A" in Form1 from Module1, I couldn't just say "Call A" – I would have to say "Call Form1.A".  Public procedures should be reserved for standard modules; standard modules are typically where you place code that is shared by the various modules in the project.  References to Public procedures of standard modules need not be qualified.

 

The figure below attempts to highlight some of the concepts discussed above:

 

PROJECT

(Form1)

Public Class Form1

 

Private Sub A()

  Call B     'OK – B is in same module      

  Call C     'OK – C is Public in MODULE1

  Call D     'Error – D is Private to MODULE1 

End Sub

 

Public Sub B()

  Call A     'OK – A is in same module

  Call C     'OK – C is Public in MODULE1

  Call D     'Error – D is Private to MODULE1

End Sub

 

End Class

(Module1)

Module Module1


Public Sub C()

  Call Form1.A   'Error – A is Private to Form1

  Call Form1.B   'OK – B is Public in Form1

  Call D         'OK – D is in same module

End Sub

 

Private Sub D()

  Call Form1.A   'Error – A is Private to Form1

  Call Form1.B   'OK – B is Public in Form1

  Call C         'OK – C is in same module

End Sub

 

End Module

 

In addition to Public and Private, scope for a Sub or Function procedure may also be specified as Protected, Friend, or Protected Friend. However, the examples presented here will use only Public or Private scope.

 

Note from the table above that a Sub procedure consists of a procedure header in the format:

 

            [Public | Private | Protected | Friend | Protected Friend] Sub ProcedureName()

 

followed by the code that makes up that procedure, followed by the statement End Sub.  As you will discover later, the procedure header can get more complicated than this. As you saw previously, when you want to create an event procedure, VB will automatically generate the "stub" for the event procedure (it will generate the procedure header and the End Sub statement, and you just fill in the code in-between).  These event procedures are always Private in scope.  When you create a programmer-defined procedure, if you omit the word Private or Public, the default is Public.

 

Note: Lines of code beginning with an apostrophe (') are regarded as comments, and are ignored and not executed by VB.  Comments can also occur in the middle of a line (as in the previous example).  Once VB sees an apostrophe in a line, the remainder of that line is ignored. (An exception to this is when the apostrophe is part of a quoted string.)