4.4. Implementing Custom Class Methods

Class methods are implemented by creating subroutines (also known as sub procedures) and functions within the class. There is no practical difference between creating a subroutine or function in a class module or creating it in a code or form module. If you're used to programming in another language such as C or C++, the concept of a subroutine will be new to you, although it's analogous to a void function.

VB functions and subroutines differ only in the ability of a function to return a value to the calling procedure, and thus a function call can be placed on the right side of an assignment statement. However, I always recommend that modules consist only of functions, not of subroutines. Why? Since they are identical except that functions return a value, you can use functions to improve the robustness of your application by always returning at least a Boolean value indicating whether the function has succeeded or failed. The calling procedure isn't forced to handle the return value. Look at the two code snippets below:

Call OpenFile(sFileName)

If OpenFile(sFileName) Then

The first line of code calls a function to open a particular file. Since the return value is discarded, the function has to assume that the file was opened successfully. The function call in the second line of code returns a Boolean True or False value that is handled in the code, letting you know whether or not the call to the OpenFile function was successful.

4.4.1. The Scope of Custom Methods

As with variables and properties, the scope or visibility of a class method is determined by the scope statement you use when defining the method. These are:


Private

Restricts the visibility of the method to the class module in which the method is defined.


Friend

Restricts the visibility of the method to modules in the same project as the method definition. Friend class methods appear in the IntelliSense drop-down list for the class and are made available for statement completion in other modules within the same project.


Public

Allows the method to be called from within the class module, from any other module in the project, and from outside the project. Specifying a method as public adds the method declaration to the Type Library for the class. When a reference is made to the class from another project, public class methods appear in the IntelliSense drop-down list for the class and are made available for statement completion.

4.4.2. Passing Arguments to Custom Methods

Although the way you pass arguments to methods is the same as passing arguments to other functions or subroutines, you should consider whether the class will be running in or out of the calling application's process.

If the method and calling statement are in the same process, generally you should pass arguments by reference, using the ByRef keyword. Within the same process, ByRef simply passes a reference to the variable, which in the main is more efficient that passing a copy of the variable's value, as ByVal does.

However, if there is a process boundary to be crossed, ByRef actually makes a copy of the value that it passes to the called procedure. When the called procedure terminates, it also makes a copy of the value to pass back to the calling procedure. The calling procedure then copies the returned value into the original value's address in memory. This means that if you are passing arguments across process boundaries, passing arguments by value with the ByVal keyword is more efficient.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.190.156.212