Using Procedures in Visual Basic
You might not be aware of it, but you've been working with subs and functions for a while. Event procedures such as Click() and Load() are subs, and Visual Basic comes with many predefined functions built right into it, such as LoadPicture() and Len().
Visual Basic is a procedural language--that is, you can make blocks of code that can be referred to by a name. After a block of code has a name, it can be called and executed. In other words, you can
write some lines of code, enclose them in a code block, give the block a name, and then call the block when you need it. It's almost like having a program within a program. These little programs that live within larger programs are called "functions" if they return a value and "subs" if they don't.
Programmers have written user-defined subs and functions for years. (In fact, the term "sub" is a shortened form of "subroutine" that gradually became its own word.) They make coding easier, faster, and more robust. Also, making your own subs and functions puts you on the road to writing encapsulated and reusable code. Encapsulation is simply the methods and properties of an object enclosed behind a public interface.
BUILT-IN FUNCTIONS
String Functions
Function Name: UCase
Function Description: Returns the String that is passed in all uppercase letters.
Common Uses: UCase can be used when the desired output is required to be in all uppercase letters. It is also commonly used when you wish to validate data entered by a user against a given string.
Syntax: String = UCase(String)
Examples:
UCase(“Input String”) returns “INPUT STRING”
UCase(“all lowercase”) returns “ALL LOWERCASE”
Previous Way of Coding Validation:
If (txtVote.Text = “Bush” Or txtVote.Text = “BUSH” Or _ txtVote.Text = “bush” Then ...
If (UCase(txtVote.Text) = “BUSH”) Then ...
Function Name: LCase
Function Description: Returns the String that is passed in all lowercase letters.
Common Uses: LCase is very similar in use to UCase.
Syntax: String = LCase(String)
Examples:
UCase(“Input String”) returns “input string”
UCase(“all lowercase”) returns “all lowercase”
Function Name: Trim
Function Description: Returns a String with the same content, except the leading and trailing spaces are removed.
Common Uses: Often when data is gathered, additional spaces may exist before the first noncharacter or after the last nonblank character.
It is good practice to remove these so that data may be presented cleanly.
Syntax: String = Trim(String)
Examples:
Trim(“ InputString”) returns “InputString”
Trim(“InputString ”) returns “InputString”
The following code will initialize two Strings.
One will contain a String that has the leading and trailing spaces removed by the Trim function.
It is displayed between two vertical bars so that it will be obvious that the spaces have been removed.
The second String will be created in a similar manner; however, the spaces will not be removed.
Dim strTest As String
Dim strWithBlanks As String
Dim strBorder As String
Dim strTrimmedOutput As String
Dim strUnTrimmedOutput As String
strTest = " Hello " 'Two spaces before and after
strBorder = "|"
strTrimmedOutput = strBorder & Trim(strTest) & strBorder
strUnTrimmedOutput = strBorder & strTest & strBorder
MsgBox(strTrimmedOutput)
MsgBox(strUnTrimmedOutput)
Function Name: Space
Function Description: Returns a String containing the number of spaces indicated by the parameter.
Common Uses: Often you wish to add spaces to set the total length of a String to an exact size.
This is often used when working with fixed-width data files.
Syntax: String = Space(Integer)
Examples:
Space(5) retruns “ “
Space(10) returns “ “
Function Name: Len
Function Description: Returns the number of characters contained in a String
Common Uses: Len is used to determine the size of a String.
Syntax: Integer = Len(String)
Examples:
Len(“Inconceivable”) returns 3
Len(“Iocaine Powder”) returns 14
Function Name: Left
Function Description: Returns the first N characters of a String where N is an Integer parameter indicating the number of characters to return.
If N is greater than the number of characters in the String, then the String is returned.
No extra spaces are added.
Common Uses: Often you are only concerned with the first few characters of a String. Left is a great way to look at only the beginning of a String.
Syntax: String = Microsoft.VisualBasic.Left(String, Integer)
Examples:
Left(“Beginning of String”, 5) returns “Begin”
Left(“Beginning of String”, 2) returns “Be”
Function Name: Right
Function Description: Returns the last N characters of a String where N is an Integer parameter indicating the number of characters to return.
If N is greater than the number of characters in the String, then the String is returned.
No extra spaces are added.
Common Uses: Often you are only concerned with the last few characters of a String. Right is a great way to look at only the end of a String.
Syntax: String = Right(String, Integer)
Examples:
Right(“Ending of String”, 5) returns “tring”
Right(“Ending of String”, 2) returns “ng”
Function Name: Mid
Function Description: Returns a specific number of characters of a String allowing the developer to indicate where to start and how many characters to return.
The first parameter is the source String.
The second is an Integer indicating the starting position to copy from.
The third parameter is optional and indicates the number of characters to copy.
If the third parameter is left out, all characters from the starting position are returned.
Common Uses: Often you wish to extract a portion of a String to use separately from the rest of the String. This is often the case when working with fixed-width data files.
Syntax: String = Mid(String, Starting Position, Optional Length)
Examples:
Mid(“This is the String”, 6, 2) returns “is”
Mid(“This is the String”, 9, 3) returns “the”
Function Name: InStr
Function Description: Returns the position of the first occurrence of a substring that is searched for in the String passed.
InStr(“ab ab ab”, “a”) returns 1
InStr(“ab ab ab”, “c”) returns 0
Common Uses: InStr can be used to tell us if a String has a certain substring contained within it. It operates much like searching a document for a word.
Examples:
InStr(“This is a very”, “is”) retruns 3
InStr(“ab ab ab”, “ab”) returns 1