From the course: Visual Basic Essential Training

Understand procedures and methods - Visual Basic Tutorial

From the course: Visual Basic Essential Training

Start my 1-month free trial

Understand procedures and methods

- [Instructor] As your applications get larger, you'll need to organize your code. And Visual Basic gives you a coupla ways of doing that. This is the chapter where we explore how to break your application into logical, self-contained code areas. We'll start with a look at all the parts and learn the terms used in the Visual Basic world. We'll start with procedures. A procedure is a block of Visual Basic statements enclosed by a declaration statement. Declaration statements start with a category of the procedure and finish with a matching end procedure statement. There are four types of procedures. Function procedures, Sub procedures, Property procedures, and Operator procedures. A procedure must have a valid name, and that name must be unique within it's name space. Choosing a good name is important, it should describe what the procedure does, or what it returns to the caller. We invoke the procedure from some other place in the code. This is known as a procedure call. We call it by using the procedure name. Some procedures are typed. A function procedure uses the type to indicate what data type is returned from the function. The property procedure uses the type to indicate what data type is stored in the property. Procedures are defined in modules or in classes. In this example, I have a module that defines the GetFileName function, and I have a class that defines the CalculateFlightPath function. Sub and Function procedures within classes are often called methods. Procedures and methods are terms that are used interchangeably. You'll hear developers say call a function or invoke a method. There are three ways to exit a procedure. In this sub, the most obvious way to end is to run all the code in the procedure and arrive at the End Sub statement. The second way is to write a return statement in the code. This is usually the last line in a function and is where you return the results to the caller, but it doesn't have to be the last line, and it can be used in Sub and other procedures too. It states that it's time to return to the caller. Statements following the return statement do not run. In this case, the last code line will never run. The third way is write an exit statement in the code. This is similar to a return. Statements following an exit statement do not run. Use Return when you need to return data to the caller. You'll find that in a Function or a Property procedure. Use Exit when there is no data to return. That would be in a Sub or an Operator. Now let's look at what each kind of procedure is used for. A Function is used to perform some actions and return data. A Sub performs the action and returns no data. A Property is used to access data that is stored within the module or the class. An Operator procedure is a series of Visual Basic statements that define the behavior of a standard operator. The name given to an Operator's often a symbol, like the equal sign, the plus and minus, multiply and divide symbols, or it could be some common logical operator names like or and or not. Operators are important for numeric classes. For ease of use, we use the plus symbol for addition and the minus symbol for subtraction, but there has to be code that runs to perform the operations. The double class defines the operator to work with double values, the Boolean class defines the operator to work with Boolean values. It's unlikely that you'll need operators for the classes you'll build, but I've included it for a completeness. All executable statements in Visual Basic must be within some procedure. Now that we've explored the technology, let's explore some code.

Contents