Function) is a process with a return value. VB. NET functions include built-in and custom functions of the system. Built-in functions are defined by the system and can be directly used in programs. For example, the Now and CInt functions used in the previous example are built-in functions. User-defined functions are defined according to the function definition syntax as needed.
1. User-Defined Functions
Syntax:
[Private | Public] Function procedure name (parameter table) [As data type]
Statement Block
Function name = return value
[Exit Function]
[Statement block]
End Function
The syntax format of the function definition is similar to that of the Sub subprogram. There are three differences:
? The start and End signs of the Function definition are Function and End Function;
? The Function Definition header contains the "As data type" section to describe the type of the function return value;
? A function must have a statement that returns the function value. The Return function value statement can be in the following two forms: 1) function name = expression; 2) Return expression.
For example, the following program defines a non-argument function GetPi, which returns the circumference rate value:
Function GetPi () As Double
Dim Pi As Double = 3.14
GetPi = Pi
End Function
For another example, the following function returns the circular area, and the parameter r is the circle radius:
Function Area (ByVal r As Double) As Double
Dim Pi As Double = 3.14
Return Pi * r
End Function
2. function call
The call format of the function is the same as that of the Sub subprogram, that is, the real parameter table is added after the function name. For example, the following statement calls the Area function:
TxtArea. Text = CStr (Area (5) 'calculates the circular Area with a radius of 5 and displays
The following statements call the GetPi function:
Dim Pi As Double = GetPi () 'defines the variable Pi and calls the function to assign the initial value to it.
Call GetPi () 'cannot use the return value of the Function
It can be seen that when a function is called in the Call format, the return value of the function cannot be used. Therefore, this format is rarely used. In addition, for a non-argument function, a pair of parentheses after the function name cannot be omitted during the call, which is different from the Sun subroutine call.
Example 3.10: 1 ~ 1 ~ of 6 ~ The value is a value of 6, as shown in 3-18.
|
| Figure 3-18 computing power functions and their calls |
Source code is as follows Ex3-10.aspx ):
3. built-in functions
The built-in functions of VB. NET are rich and provide great convenience for programming. Built-in functions can be divided into the following six categories:
(1) Data Type Functions
Including obtaining data types, checking data types, and converting data type functions. Common functions include:
CChar (exp): converts a parameter to Char type data. The parameter must be 0 ~ 65535.
CDate (exp): converts a parameter to the Date type. The parameter must be a valid Date and time representation method.
CInt (exp): converts a parameter to Integer type data. The parameter must be-2147483648 ~ An integer between 2147483647 and the decimal part is rounded.
CLng (exp): converts a parameter to Long type data. The parameter must be-9223372036854775808 ~ An integer between 9223372036854775807 and the decimal part is rounded.
CStr (exp): converts a parameter to String type data. If the parameter is of the Boolean type, the return value is "True" or "False". If the parameter is of the Date type, the return value is displayed in the system's short Date format.
Asc (var) and AscW (var): The var parameter can be Char or String type data, and the return value is the ASCII code of the first character of the character or String. For example, Asc ('1') returns 49, Asc ('20140901') also returns 49.
Chr (int): If the int parameter is an Integer data type, the function returns ASCII characters that are the parameter values. For example, Chr (97) will return the character 'a '.
Int (num): return the integer part of the parameter. If the value of the parameter is less than 0, the first negative number that is less than or equal to the parameter is returned. For example, Int (2.8) returns 2, Int (-2.8) returns-3.
(2) mathematical functions
The Math function is located in the Math class under the System. Math Namespace. Therefore, you must use <% @ Import Namespace = "System. Math" %> to Import the Math in a mathematical function program. Common mathematical functions include:
Abs (num): returns the absolute value of a parameter. For example, Abs (-2) returns 2.
Exp (num): returns the num power of e.
Round (num): returns the result after rounding the value of the num parameter. For example, Round (3.2) returns 3.
Sign (num): symbol function, return the positive and negative numbers of parameters. If num> 0, return 1; if num <0, return-1; if num = 0, return 0.
Sqrt (num): returns the square root of a parameter.
Rnd [(num)]: returns a value ranging from 0 ~ A random number between 1. The parameter and return value are of the Single type. If the num parameter is <0, the same random number is generated each time. If num> 0, the next random number is generated in sequence. This is the default value. If num = 0, returns the most recent random number. Before using Rnd to generate a random number, it is best to use the Randomize statement to generate a random number.
(3) Date and Time Functions
Including obtaining/setting time, returning date and time, and calculating date and time functions. Common functions include:
Now (): returns the current date and time of the system.
Today (): returns or the current date of the system.
DateSerial (year As Integer, month As Integer, day As Integer) As DateTime: returns the date that is concatenated by three parameters. The year parameter must be 1 ~ 1999 or 0 ~ An integer between 99 ~ 99, is interpreted as 1930 ~ 2029; month must be 1 ~ An integer between 12. day must be 1 ~ An integer between 31.
DateValue (str As String) As DateTime: converts a String to a date. The str parameter must be a string expression from 00:00:00, January 1, January 1 to 23:59:59, January 1, December 31, 9999. For example, DateValue ("February 12 2005") returns #2005/2/12 #.
TimeValue (str As String) As DateTime: converts a String to a time. The str parameter must be a string expression from 00:00:00, January 1, January 1 to 23:59:59, January 1, December 31, 9999. For example, TimeValue ("9:15:20 AM") will return #09:15:20 AM #.
MonthName (month As Integer) As String: gets the name of a month. For example, MonthName (1) will return "January ".
WeekdayName (weekday As Integer) As String: gets the name of the day of the week.
DateAdd (interval As DateInterval, num As Double, dt As DateTime) As DateTime: increase or decrease of date and time. Here, DateInterval is the enumerated value, indicating the unit of increase or decrease of date and time. The values include:
DateInterval. Year: Year
DateInterval. Month: Month
DateInterval. Day: Day
DateInterval. WeekOfYear: week of the year
DateInterval. Minute: Minute
DateInterval. Quarter: Quarter
DateInterval. DayOfYear: The day of the year
DateInterval. Hour: Hour
DateInterval. DayOfWeek: day of the week
DateInterval. Second: seconds
For example, DateAdd (DateInterval. Quarter, 2, #2005/3/20 #) returns #2005/9/20 #, that is, #2005/3/20 # plus two seasons.
DatePart (interval As DateInterval, dt As DateTime) As Integer: return the specified part of the date and time. For example, DatePart (DateInterval. Year, #2005/3/20 #) returns 2005.
DateDiff (interval As DateInterval, dt1 As DateTime, dt2 As DateTime) As Long: calculates the time difference between dates, returns the interval between dt1 and dt2, in interval. For example, DateDiff (DateInterval. Day, #12/5/99 #, #12/15/99 #) returns 10, that is, the interval between two dates is 10 days.
(4) string functions
Including String Conversion, string SEARCH, comparison, filtering, and replacement functions. Common functions include:
LCase (str): converts parameters to lowercase letters. For example, LCase ("Hello") will return "hello ".
UCase (str): converts parameters to uppercase letters.
StrComp (str1 As String, str2 As String) As Integer: String comparison. If str1 is smaller than str2,-1 is returned. If str1 is greater than str2, 1 is returned. If str1 is equal to str2, returns 0.
Replace (str As String, find As String, replacement As String) As String: Replace the find substring in str with the replacement String. Example: Replace ("You are right! "," You "," We ")," We are right! ".
InStr (Start As Integer, str1 As String, str2 As String) As Integer: returns the position where the str2 String first appears in the str1 String. For example, InStr (1, "Hello", "lo") returns 3.
Join (source () As String: combines a String array into a single String.
Len (str): returns the length of the string.
Left (str, int): returns the int characters on the Left of the str string. For example, Left ("You are right! ", 2)" Yo "will be returned ".
Right (str, int): return the int characters on the Right of str.
LTrim (str), RTrim (str), and Trim (str): delete spaces in the string. LTrim (str) is the space on the left of the string to be deleted; RTrim (str) is the space on the right of the string to be deleted; Trim (str) is the space on both sides of the string to be deleted.
Mid (str, start [, length]): returns the string of length starting from the character specified by the start parameter in str. If length is not specified, all characters after start are returned. For example, Mid ("You are right! ", 9, 5) Return" right ".
(5) Formatting Functions
Including Date and Time formatting, currency value formatting, value formatting, and percentage formatting functions.
(6) array processing functions
Including LBound and UBound.
Appendix B lists common built-in functions of VB. NET. The following is an example.
Example 3.11: a program is designed to calculate the number of days and time remaining from the specified date. As shown in figure 3-19, the user enters the specified date in the format of # MM/DD/YYYY #. After clicking "Submit", the program displays the remaining days and times on the page.
|
| Figure 3-19 Date and Time Calculation Program |
Source code is as follows Ex3-11.aspx ):
Date and Time Calculation
|
| BibliographyPrevious sectionNext section |