New FunctionsA total of fourteen new functions have been added to Visual Basic's already impressive repertoire of built-in commands and procedures. Strangely, they all have to do with strings in some fashion. The new functions are described briefly in the following sections. If you want to find more specific information about a particular function, consult VB's online Help system or the Visual Basic reference manual. CallByName Function This function enables you to use string values to specify the names of properties or methods at runtime, rather than hard-coding the names into the code. For example, if you wanted to set the Visible property of the txtInfo TextBox control, you would have to hardcode it like this in previous versions of Visual Basic: txtInfo.Visible = True With the new CallByName function, you can use strings to specify the property name: CallByName txtInfo, "Visible", vbLet, True You can also read property settings or execute methods with the CallByName function. However, you cannot use a string value to specify the name of the object that has the property or method. Filter Function The Filter function, when passed a one-dimensional array, will return a subset of that array that includes only those items that contain a given search string. Alternatively, the subset array can include items that don't contain the given search string. Strings can be compared using either a binary or textual comparison method, or the setting of the Option Compare statement can be used. FormatCurrency Function Programmers who routinely need to display monetary values will find the new FormatCurrency function a welcome addition. Simply put, the FormatCurrency function converts a given expression into a currency format. The parameters of the function enable you to specify how many digit positions there will be after the decimal point, whether or not a leading zero is displayed for fractional values, whether or not negative values should be placed within parentheses, and whether or not digits should be grouped together where appropriate. The way in which numbers are formatted depends a lot on the regional settings for the machine on which the FormatCurrency function is used. In the United States, for example, the number would likely be displayed with a preceding dollar sign, and digit grouping (if enabled) would use commas (i.e., "$32,768.00"). FormatDateTime Function This function returns an expression that is formatted as a date and/or time value when given a valid date expression. You can choose to display long ("Friday, February 06, 1998") or short ("2/6/68") date values, and you can display time using 12-hour or 24-hour formats. Regional settings for the computer on which the FormatDateTime function is used will influence the way in which dates and times are formatted. FormatNumber Function The FormatNumber function is almost identical to the FormatCurrency function, except the formatted number that is returned does not include a preceding monetary symbol (such as a dollar sign). Again, a computer's regional settings will dictate how a number is formatted by this function. FormatPercent Function The FormatPercent function returns the formatted percentage value for a given expression. For example, passing a value of .75 to this function might result in a formatted value of "75%." All of the same parameters that are found in the FormatCurrency and FormatNumber functions can also be applied to the FormatPercent function, so you can specify things such as a fixed number of digits after the decimal point (if any). InstrRev Function The InstrRev function performs the same task as the regular Instr function: it returns the character position of a given search string within a second string. The difference is that the InstrRev function works in reverse and starts searching from the end of the second string rather than from the beginning. Join Function The Join function is passed X a one-dimensional array and returns a string that contains the items of the array concatenated (joined) together. You can specify that the array items be delimited by a given string character, or you can specify a zero-length string ("") so no delimiters are used. MonthName Function The MonthName function is very simple: pass it a number from 1 to 12 and it will return the name of the corresponding month ("January", "February", etc.). You can also specify that short month names ("Jan", "Feb") be returned instead. Replace Function The Replace function acts as a simple search and replace operation. Pass it the string to search, the string to find and another string with which to substitute it, and the Replace function does the rest. You can optionally specify how many times to perform the string substitution and whether strings should be compared using a binary or textual comparison method. Round Function This is one simple function that should have been added to Visual Basic a long time ago. The Round function returns the value of a given expression, rounded off to a specified number of decimal places. Split Function The Split function performs the opposite of the new Join function. Rather than concatenate elements of an array into a single string, the Split function divides up a string into separate items and returns them as a one-dimensional array. A delimiter character can be specified so the Split function knows where items in the string start and stop. If no delimiter character is provided, then Split uses the space character as the delimiter. Among other uses, this function can come in handy when you need to parse a sentence into individual words. StrReverse Function The StrReverse function returns a given string in reverse order. Therefore, the string "Visual Basic" would be returned by the StrReverse function as "cisaB lausiV". WeekdayName Function To convert a numeric value of 1 to 7 into the corresponding day of the week (1 = "Sunday", 2 = "Monday", etc.), use the WeekdayName function. Though the full day name is returned by this function, you can optionally specify that the WeekdayName function returns the abbreviated name (i.e., "Sun", "Mon") instead. If your numbering system for days of the week does not begin with Sunday, you can specify a different day as being the first weekday. |
|
![]() |
![]() |
|||