Coding the Application
In any Visual Basic application, the Visual Basic programming language will be used to create program code. The task of the programmer is to provide a solution to a problem in the most economical way. In order to write efficient code that works, the programmer must be familiar with their chosen programming language in terms of its built-in commands, syntax and grammar. This kind of knowledge takes time to acquire, and involves frequent reference to appropriate literature (such as the Microsoft Visual Basic 6.0 Programmer's Guide). Some of the main elements of the Visual Basic programming language are described below.
- Visual Basic commands - these are the commands that are built in to the Visual Basic language, and they are used to carry out various tasks (file I/O, processing user input, manipulating strings, etc.).
- Variables - variables are named data items, such as integers, floating point numbers and strings, that are stored in memory and used by your application - as the name suggests, the value of a particular variable can change.
- Constants - constants are named data items, stored in memory, the value of which cannot be changed. Visual Basic has a number of (very useful) built-in constants.
- Functions - a function is a named block of program code (or procedure) that runs when the function is called, and returns a value (which is stored in a variable) to the procedure that calls it. A function can accept one or more arguments (parameters). The square root function (SQR), for example, accepts a numerical value as its argument and returns the square root of that value to the calling routine.
- Subroutines - a subroutine is another type of proceedure, although it does not return a value, unlike a function. A procedure may or may not change the value of variables that are passed to it as arguments.
Most of the program code you write will be event procedures which will execute in response to the occurrence of various events. Code does not have to reside inside an event procedure, but is always called from within an event procedure. If you double-click on an object in the IDE you'll be presented with the code window for that object. The two dropdown lists available in the code window show the list of available objects in the application, and the events supported by each object. The item General in the first dropdown list refers to the section in which VB automatically places any user-defined functions and subroutines. An event procedure attached to a specific object is (usually) only executed when the specified event occurs, whereas a function or subroutine can be called from any event procedure in the application.