All languages have subroutines. These are small bits of code that are used to accomplish pieces of the whole program. There are some examples of these to try. In VB there are two kinds. Subroutines (Sub) are routines that don't return a value. Functions (Function) do return a value.
Subroutines can have any number or type of parameters and their names are controlled by the same rules as variables.Here is an example of a subroutine taken from the examples above. Next to it is the C++ equivalent.
' convert Kelvin to Celsius Private Sub getcelsius(ByVal degreesk As Double, ByRef degreesc As Double) degreesc = degreesk - CONV End Sub |
// convert Kelvin to Celsius void getcelsius(double degreesk , double & degreesc) { degreesc = degreesk - CONV; } |
Note that the first parameter to the Sub is marked with ByVal. This indicates that the parameter is being passed by value. That is, the formal parameter, degreesk, contains a copy of the actual parameter that it was called with. Changing degreesk has no effect on the actual parameter. The other parameter, degreesc, is marked as ByRef. This means that the formal parameter acts like another name for the actual parameter. So changing degreesc will change the actual parameter. This allows Subs to send back values by altering the values of the parameters. Subs are almost always called without parenthesis like this.
getcelsius Val(kelvins.Text), degreesc
Functions return a value to the calling code. They can also have value and reference parameters.
' convert Celsius to Kelvin Private Function getkelvins(ByVal degreesc As Double) As Double getkelvins = degreesc + CONV End Function |
// convert Celsius to Kelvin double getkelvins(double degreesc) { return(degreesc + CONV); } |
Private Function tryit(foo As Integer) As Integer If foo > 3 Then tryit = 7 Else tryit = 9 End If Debug.Print "shouldn't see this" End FunctionThis will print the "shouldn't see this" line BEFORE it returns the 7 or 9 to the caller.
Since you could have ByRef parameters in a function, a function could also send back values through the parameters. Function calls in VB do use parenthesis like this.
Dim temp As Double temp = getkelvins(32.0)There are some examples of function returns and an attempt to misuse the reference parameters.
VB provides a bunch of information about your project in the App object. We have seen the Path property already, but here are some others.
Comments | A string containing comments about the application. Read only at run time. |
---|---|
CompanyName | A string containing the company or name. Read only at run time. |
EXEName | A string containing the filename of the EXE without the extension. Read only. |
FileDescription | A string containing briefly describing applications purpose. Read only at run time. |
HelpFile | A string containing help file associated with the application. Read and write at run time. |
LegalCopyright | A string containing copyright notification. Read only at run time. |
LegalTrademarks | A string containing trademark information. Read only at run time. |
Major | A string containing major version number. Read only at run time. |
Minor | A string containing minor version number. Read only at run time. |
Path | A string containing the directory from which the application started. Read only at run time. |
PrevInstance | Returns a value if an instance of the application is already running. Read only at run time. |
ProductName | A string containing the assigned product name of the application. Read only at run time. |
Revision | A string containing the revision number of the application. Read only at run time. |
When working with records, it can be tedious to type the variable name over and over. Especially if it is part of some larger object, like an array. So VB has the With statement that provides some shorthand.
Type foo f1 As String f2 As String f3 As Integer End Type Dim a(10) As foo With a(i) .f1 = "hello" .f2 = "hi" .f3 = 42 End With
You can change the size of arrays while the program is running. If you think this is likely, it is best to create the array with no space and initialize it later. Like this
Dim a() As Integer ReDim a(9)This leaves you with an empty 10 (0 to 9) element array of integers. Later, if you need it to be bigger, do this
ReDim Preserve a(15)Notice the Preserve option. With this, the old values in a are retained. Without it, they go away.