Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
This chapter is from the book

Writing Your Own Routines

Although the built-in functions are quite useful, there will always be times when you need to create your own routines. Perhaps you need to select a set of built-in functions that are all called in the same way, or perhaps you need to create some unique functionality. Either way, Visual Basic .NET makes creating your own routines easy.

There are two types of routines used in Visual Basic .NET. One type is a routine that does something, but doesn't return any value. These are called subroutines (or sub for short). The other type of procedure does something, but returns a value. These are called functions.

Subroutines

A subroutine is a block of Visual Basic .NET code that performs some task—for example, the Console.WriteLine method that you see in many of the examples. It prints information to the screen, but does not return any value. You use subroutines to perform tasks in your programs.

Generally, any time you have some code that you perform more than once, you should think about putting it into a subroutine. Similarly, if you have some code that you might use in multiple applications, you should put it into a subroutine. Subroutines let you isolate a small chunk of your program, so that rather than repeating the whole block of code, you simply refer to it by name. This does not mean that the subroutine will always do exactly the same steps, but that it will do some task. For example, a recipe might say, "Add one part vinegar to three parts oil." One time you might mix up one cup of vinegar to three cups of oil, whereas another time it might be only three tablespoons of vinegar to nine tablespoons of oil. Either way, you have performed the CreateVinaigrette subroutine. Yes, I'm making dinner as I write this.

To create your own subroutines, you use the Sub keyword:

Sub SubRoutineName(Parameter1 As Type, Parameter2 As Type, ... 
ParameterN As Type)
 'Do whatever in here
End Sub

In this syntax, each of the Parameters defines a value that is to be passed into the routine. Listing 3.2 shows declaring and using a subroutine.

Listing 3.2 Creating and Using a Subroutine

 1 Sub ShowMessage(ByVal Message As String)
 2   Console.WriteLine(Message)
 3 End Sub

 4 ShowMessage("Hello World from Visual Basic .NET")

Our subroutine begins with the Sub keyword, as on line 1. The subroutine is called ShowMessage, and takes one parameter when you call it. The subroutine ends with the End Sub keyword (line 3). In between is the actual code executed by the subroutine. In this case, it simply displays the contents of the parameter to the Console window. Line 4 shows one possible way of calling the subroutine, passing in the string "Hello World from Visual Basic .NET".

Functions

Creating your own functions enables you to create new capabilities within your application. Creating a new function is similar to defining new subroutines, except that you define the return value type. Within the procedure, you identify the value to be returned, as shown here:

Function FunctionName(Parameter1 As Type, ... ParameterN As Type) As ReturnType
 'Do whatever in here
 Return ReturnValue
End Function

In this syntax, each of the parameters defines a value that is to be passed into the routine, ReturnType is the data type the function returns, and ReturnValue is the value that will be returned from the function. Listing 3.3 shows the declaration and use of a simple function.

Listing 3.3 Creating and Using a Function

1 Function Volume(ByVal Length As Integer, _
2     ByVal Width As Integer, ByVal Height As Integer) As Integer
3  Return Length * Width * Height
4 End Function
5
6 Console.WriteLine(Volume(3,4,5))

Scope

Scope is one of those lovely computer-speak words that means, "Who else can see me?" Formally, scope defines the visibility of variables within a program, that is, which routines could use a given variable. You might not want all routines to access all variables. Allowing all routines to see all variables could lead to one routine "accidentally" changing the value of a variable, introducing a bug in your program.

Up until now, we have usually declared variables using the Dim keyword inside of procedures. However, you can also declare variables outside of procedures to make the variable available to multiple procedures. If you do this, you can use two other keywords, Public and Private:

  • Public variables are available throughout an application. These are Global variables, which exist globally, or throughout, the application. Public variables should be used sparingly, but are useful when you need some value that will be used at many points in your program, such as the connection to a database, or a file.

  • Private variables are available within the module or class where they are declared. Private variables are used frequently in applications when you need a single variable that can be used in multiple procedures. By creating it with the Private keyword, you allow all the procedures within one module or class to access the variable. Private variables are useful for sharing common information required for a task, such as an intermediate value that can be accessed by different functions to perform a calculation.

TIP

When you create a variable, you should declare it a newly created variable as close as possible to where it is needed. If you only use a variable in one procedure, you should declare that variable within that procedure.

Use module-level Private and Public variables sparingly.

Why Is Scope Important?

Scope enables you to isolate the data used by your applications' procedures. Much older versions of BASIC did not have the capability for scope, and all variables were accessible and changeable from all parts of the program. Imagine writing a program back then—you might often reuse (on purpose or accidentally) a variable elsewhere in a program. This could possibly lead to a bug if you changed the value in one spot in the program, only to mistakenly read the changed value later when you expected to get the original value.

Scope and Procedures

Just as variables can have scope, procedures (subroutines and functions) have scope. Scope for procedures means the same as scope for variables: It describes where else in your program you can use the procedure (or outside your program, as you'll see when you begin creating objects).

Procedure scope is defined using the same keywords used for variable scope. Generally, scope has the same meaning here as well.

  • Public The procedure can be called from any other part of the application. This is the default if you don't add any other keyword.

  • Private The procedure can only be called from another procedure within the same module or class where it is defined. This is useful when you are writing a number of support routines used throughout a calculation, but that other routines would not need to use.

Just as with variables, additional scope keywords apply when you are creating objects in Visual Basic .NET. We'll look at those keywords later.

  • + Share This
  • 🔖 Save To Your Account