This final section includes a brief listing of the functions built-in to the Visual Basic language, many of which you will use regularly in your applications. Also listed here are some members of the FCL that replicate features that were part of the Visual Basic language before .NET, but were moved into the Framework for more general access. For the exact syntax required to use these functions, access the Visual Studio online help.
The conversion functions allow you to convert data of one Visual Basic data type to another. It's not a free-for-all, so don't go converting the string "hello" to an integer and expect it to work. But converting numbers from one numeric type to another, or converting values between string and numeric types, generally work just fine.
All of these statements (except CType) have the same basic syntax:
dest = CXxxx(source)
where source is the value to be converted by CXxxx. You don't have to assign the result to a variable; you can use the result anywhere you would use a similar literal or variable value. Table 6-9 lists the built-in conversion functions.
Table 6-9. Visual Basic Conversion Functions
Converts a value to a Boolean.
Converts a value to a Byte.
Converts a value to a Char. If the source value is a string, only the first character is converted.
Converts a value to a Date. If the source value is a string, it must be in a valid date or time format.
Converts a value to a Double.
Converts a value to a Decimal.
Converts a value to an Integer.
Converts a value to a Long.
Converts a value to an Object. This is useful when you want to store a value type as an Object instance.
Converts a value to an SByte.
Converts a value to a Short.
Converts a value to a Single.
Converts a value to any defined type, class, or interface, either in your application or in the FCL. The syntax is:
where newType is a data type. For instance:
converts the Integer 5 to a String. As with other conversion functions, you can't convert data from one type to another if the types are incompatible, or if there is no conversion available that knows how to generate the target type from the source type. Operator overloading, discussed in Chapter 12, provides a way to let the CType function convert between types that would otherwise be incompatible.
Converts a value to a UInteger.
Converts a value to a ULong.
Converts a value to a UShort.
Visual Basic includes several functions designed to manage date and time values. Table 6-10 lists these functions. Most of these functions accept one or more source arguments, and return either Date, String, or numeric result.
Table 6-10. Visual Basic Date-Related Functions and Properties
Adds or subtracts time or date value to a starting date. For instance, you can add 12 minutes, or subtract three years, from a given date.
Returns the difference between two date or time values. You can specify the interval, such as months or seconds.
Returns one component of a date or time, such as the hour or the year.
Returns a Date built from specific month, day, and year values.
Returns the current date as a string. You can also set the date on the local computer using this keyword.
Returns the date portion of a combined date and time value; the time portion is discarded.
Returns the day from a given date value.
Formats a given date or time as a string, using a small set of predefined formats. This function is included for backward compatibility with older VBScript code.
Returns the hour from a given time value.
Indicates whether the data supplied to this function is a valid date or not.
Returns the minute from a given time value.
Returns the month from a given date value.
Returns the name of a month for a numeric month value.
Returns the current date and time. Equivalent to TimeOfDay.
Returns the seconds from a given time value.
Returns the current date and time. Equivalent to Now.
Returns the number of seconds that have elapsed since midnight of the current day. This function is reset to zero each midnight.
Returns a Date built from specific hour, minute, and second values.
Returns the current time as a string. You can also set the time on the local computer using this keyword.
Returns the time portion of a combined date and time value; the date portion is discarded.
Returns the current date.
Returns an integer that indicates the day of the week.
Returns the name of a weekday for an integer day of the week.
Returns the year from a given date value.
Variables created as System.DateTime (or Visual Basic Date) each include several properties and methods that provide features similar to the functions listed in Table 6-10. For instance, the Second property returns the number of seconds.
Dim meetingTime As Date meetingTime = #11/7/2005 8:00:03am# MsgBox(meetingTime.Second) ' Displays '3' MsgBox(Second(meetingTime)) ' Also displays '3'
You can use either the intrinsic Visual Basic functions or the equivalent System.DateTime methods and properties in your code. Each technique provides the same result.
Visual Basic programmers just love working with numbers; it's in their blood. Fortunately, Visual Basic includes lots of features for working wonders with numbers. In addition to the standard data manipulation operators, Table 6-11 lists several number-related functions.
Table 6-11. Visual Basic Number-Related Functions
Truncates the decimal portion of a number, returning only the whole portion. Similar to the Int function.
Formats a given number as a currency value, using a small set of predefined formats. This function is included for backward compatibility with older VBScript code.
Formats a given number as a general number, using a small set of predefined formats. This function is included for backward compatibility with older VBScript code.
Formats a given number as a percentage, using a small set of predefined formats. This function is included for backward compatibility with older VBScript code.
Formats a number as hexadecimal, and returns its string representation.
Returns the whole number that is less than or equal to the supplied value. Similar to the Fix function.
Indicates whether the data supplied to this function is a valid number or not.
Formats a number as octal, and returns its string representation.
Extracts the first valid number from a string and returns it.
The .NET Framework includes the System.Math class, which contains several math-related function members. Some of these, such as Round, Sin, and Log, were implemented as intrinsic functions in Visual Basic 6.0, but have been moved from the language to the Math class in .NET.
Visual Basic also includes several functions used for financial and accounting calculations. These functions were also included in Visual Basic 6.0. As they are not relevant to the project discussed in this book, I will only list their names here: DDB, FV, IPmt, IRR, MIRR, NPer, NPV, Pmt, PPmt, PV, Rate, SLN, and SYD.
String manipulation is a core part of Windows programming. The new XML features included with .NET are really just fancy string-manipulation routines, although with the complexities hidden from view. Visual Basic includes many functions designed to manipulate strings and characters. They are listed in Table 6-12.
Table 6-12. Visual Basic String-Related Functions
Returns the numeric ASCII or Unicode value for a character.
Given a number, these functions return the matching ASCII or Unicode character.
Returns an array that is a subset of a source array, but including only those elements that matched a pattern.
Formats number, date, and time values using predefined or custom formatting codes.
Extracts a single character from a larger string.
Returns the position of a substring within a larger string.
Returns the position of a substring within a larger string, searching from the end of the string until the beginning.
Returns a string built from a concatenation of an array of strings.
Converts a string to its lowercase equivalent.
Returns the left-most portion of a string.
Returns the length of a string.
Left-aligns a string within a larger string of spaces.
Removes spaces from the start of a string.
Extracts a substring from the middle of a larger string.
Modifies a range of characters in an existing string with new content. This is not a function, but a special Visual Basic statement. Its syntax varies considerably from that of most other Visual Basic features.
Replaces occurrences of a substring with another substring, all within a larger string.
Returns the right-most portion of a string.
Right-aligns a string within a larger string of spaces.
Removes spaces from the end of a string.
Generates a string containing a specified number of space characters. Similar to the StrDup function.
Splits a string into an array of substrings based on a delimiter.
Converts a number to its string representation.
Compares two strings, and returns an integer indicating their sort order.
Converts a string to a new format based on a conversion code. Some of the conversions involve changing the case of the content.
Generates a string containing a specified number of a given character. Similar to the Space function, but works with any character.
Reverses the characters in a string.
Removes spaces from the start and end of a string.
Converts a string to its uppercase equivalent.
As with most functions, these functions return a new string or value, leaving the original string or source values intact. The lone exception is the Mid Statement, which modifies the source string.
Variables created as System.String (or Visual Basic String) each include several properties and methods that provide features similar to the functions listed in Table 6-12. For instance, the Length property returns the number of characters in the string.
Dim simpleString As String = "abc" MsgBox(simpleString.Length) ' Displays '3' MsgBox(Len(simpleString)) ' Also displays '3'
You can use either the intrinsic Visual Basic functions or the equivalent System.String methods and properties in your code. Each technique provides the same result, although the syntax details and options may vary.
Visual Basic includes several functions that refuse to be squeezed into any of the other categories. Table 6-13 documents these functions.
Table 6-13. Visual Basic Miscellaneous Functions
Converts a value from one data type to another. Similar to the TryCast and CType functions.
Returns the string representation of an error code. This only works with the system error codes previously available in Visual Basic 6.0, although these codes are still available in .NET.
Indicates whether the data supplied to this function is a valid array or not.
Indicates whether the data supplied to this function is a valid NULL database value or not.
Indicates whether the data supplied to this function is a valid error condition or not.
Indicates whether the data supplied to this function is undefined, or set to Nothing.
Indicates whether the data supplied to this function is a reference type or a value type.
Returns a color code from a small set of predefined colors.
Returns a color code built from the individual red, green, and blue components.
Given a Visual Basic data type name, this function returns the equivalent .NET data type name.
Converts a value from one data type to another. Similar to the DirectCast and CType functions.
Returns a data type name that summarizes the data type of the supplied content. The returned string is not necessarily the name of the value's actual data type.
Returns a code indicating the general data type of the supplied content.