Modules in Visual Basic

Visual Basic Modules allow you to take your organisational skills to a whole new level.  We already have super-organised code split into nicely named Subroutines and Functions.  However, up until now, we have typically developed applications with one single Form.  Our code has sat quite happily in this Form but say we have a Subroutine to handle a click event.  How do we share this routine between multiple Forms?  We use Modules.

Create a new project called Modules.  In the Form Load event add the following code:


Dim radiusOne as Double
Dim areaOne as Double

Dim radiusTwo as Double
Dim areaTwo as Double

radiusOne = 3
radiusTwo = 2

areaOne = CalculateArea( radiusOne )
areaTwo = CalculateArea( radiusTwo )

Add this routine to the Form too:

Private Function CalculateArea (ByRef radius as Double) As Double

     Dim pi as Double

     pi = 355/113

     Return pi * radius ^ 2

End Sub

Add another Form to your Project (Select the Project with your mouse, Right click, Click Add and Click Windows Form as shown below)

new form

On this Form add:

  • a textbox called textRadius
  • a label with the text “Enter the Radius to calculate the area of a circle”
  • a label called labelArea
  • a button called “buttonCalculateArea” with the caption/text “Calculate Area”

Design the Form so it looks roughly as follows:

calculate area form

Add the following code to the button click event:

Dim radius as Double

Dim area as Double

radius = CDbl( textRadius.Text)

area = CalculateArea( radius )

labelArea.Text = “The area of the circle is “ & CStr(area)

 

What are these new functions CDbl and CStr ?  As we already know, a Textbox.Text property returns a string.  However our Function is expecting the radius passed as a Double.  The CDbl (Convert to Double) function converts the text in the TextBox into a Double data type.  If this text isn’t numeric Visual Basic will throw an error.  Once the area has been calculated by the Function CalculateArea, the CStr Function (Convert to String) converts this Double to a String,  adding it to labelArea.

 

We want this Form to be the first Form that is loaded when the application starts.  We want this Form to be the Startup Form.  To do this, click on the Project in the Solution Explorer, right click on the Form and click Properties as shown below:

visual basic project properties

You should now see the Project Properties dialog:

Set a startup form

In the Startup Form dropdown select Form2.

Go back to your project and add a new Module called ModuleFormula as shown below:

visual basic project add module

Double click on your Module to add code.  Cut and paste the CalculateArea Function from Form1 and paste it into the module.

Module ModuleFormulas

     Private Function CalculateArea (ByRef radius as Double) As Double

          Dim pi as Double

          pi = 355/113

          Return pi * radius ^ 2

     End Function

End Module

Click F5 to run your project.  What happens?  You get an error.  The error says that, ‘CalculateArea’ is not declared and may be unavailable due to its protection level.’.  Remember our exercise on Variable Scope which stated Variable scope defines when a variable can be accessed?  Functions and Subroutines have scope too.  Scope in its wider context defines when a variable, Subroutine or Function can be accessed.

The first line of our CalculateArea function is defined as follows:

Private Function CalculateArea(ByRef radius As Double) As Double

Private in this case tells Visual Basic that the Function is only available within our Module.  If we declared the Function in the Form, which we did originally, we informed Visual Basic that the Function was only accessible from code within the Form.  Essentially, Private is a means of stating that the variable/Sub/Function is local to its container.

To make the Function available to code outside of this Module, change the word Private to Public in the Function declaration.

Public Function CalculateArea(ByRef radius As Double) As Double

Press F5 and what happens?  It works! Yay!

Enter a Radius, click calculate and you’ll see the exact area (perhaps too exact) of the circle.

Now you can access this Function from anywhere in your application and you’ve started to organise the contents of your project.  You can create Visual Basic Modules with Functions and Subroutines for specific tasks.  For example, you could re-name this module moduleTrigonometry and build up the routines we need for calculating areas, radii etc.  It all depends what your application does and how you want to organise it.

You’ll notice in time that some programmers are extremely neat in organising their code and some not so much.  I recommend being disciplined at this stage until being organised becomes second nature. Your co-workers will love you for it.