VB variables, constants and data types and process overview (vi)

Source: Internet
Author: User
Tags manual integer numeric locale setting numeric value square root valid

The Numeric value stored in a Variant variable
When you store the entire number in a Variant variable, Visual Basic uses the most compact representation. For example, if you store a small number with no decimal point, the Variant uses an Integer representation of the value. If you assign a larger number thereafter, Visual Basic uses a Long value, and if it is very large or has a small number of parts, Visual Basic uses the Double value.
Sometimes you want to use a specific representation on a number. For example, to avoid rounding errors in future calculations, you might want the Variant variable to store the value as a Currency. Visual Basic provides several conversion functions that you can use to convert values to the specified type (see "Converting Data Types" earlier in this chapter). For example, use the CCur function to convert a value to a Currency type:
Payperweek = CCur (Hours * hourlypay)
If the Variant variable does not contain a number or does not contain content that can be interpreted as an amount, then a mathematical operation or function evaluation of the variant variable can be faulted. For example, you cannot perform any arithmetic operations on the value ' U2 ', although it contains a numeric character ' 2 ', but the entire value is not a valid number. It is also not possible to perform any calculation on the value 1040EZ, but +10 or -1.7E6 can be evaluated because they are valid numbers. It is often necessary to determine whether a Variant variable contains a value that can be used as a number. The IsNumeric function can accomplish such a task:
Todo
Anynumber = InputBox ("Enter A number")
Loop Until IsNumeric (anynumber)
MsgBox "The square root is:" & SQR (Anynumber)
When Visual Basic converts a non-numeric representation, such as a string containing a number, to a numeric value, it uses the locale setting (as specified in Windows Control Panel) to interpret the thousand separator, the decimal symbol, and the currency symbol.
As a result, if the country/region setting value in the Windows Control Panel is set to the United States, Canada, or Australia, the statements will return to True:isnumeric ("$")
IsNumeric ("1,560.50")
The following two statements return false:
IsNumeric ("DM100")
IsNumeric ("1,560,50")
However, if the country/region setting value in the Windows Control Panel is set to Germany, the reverse is true: the first two statements return false, and the last two statements return it.
If you assign a variant of the containing number to a string variable or property, Visual Basic automatically converts the internal representation of the number to a string. If you want to explicitly convert a number to a string, you can use the CSTR function. You can also use the Format function to convert a number to a string that contains a format such as a currency character, a thousand separator, and a decimal point symbol. The Format function automatically uses the appropriate symbol according to the Regional Settings dialog box in Windows Control Panel.
For more information, see the "Format function" in the Visual Basic 6.0 Language Reference manual and the topics about conversion functions. For more detailed information about writing applications that are distributed in foreign markets, see chapter 16th, "Internationalization."

String stored in Variant variable
Generally, storing and using a string in a Variant variable is not a problem. However, as mentioned earlier, sometimes the result of the "+" operator may be ambiguous when using two Variant values. If two variant variables contain a number, the "+" operator performs an addition operation. If the two Variant variable contains a string, the "+" operator performs a string connection. However, if a value represents a number, and another value represents a string, the situation is complicated. Visual Basic attempts to turn a string into a number first. If the conversion succeeds, the "+" operator adds the two together and, if unsuccessful, produces a "type mismatch" error.
To ensure that the action performed is a string connection, use the & operator regardless of the representation in the variable. For example, the following code:
Sub Form_Click ()
Dim X, Y
X = "6"
Y = "7"
Print X + y,x & Y
X = 6
Print X + y,x & Y
End Sub
The following results are produced on the form:
67 67
13 67
Note that Visual Basic uses Unicode to store strings internally. For more detailed information about Unicode, see Chapter 16th, "Internationalization."

The date/time value stored in a Variant variable
Variant variables can also contain date/time values. There are several functions that return date/time values.
For example, DateSerial returns the number of days left in the year.
Private Sub Form_Click ()
Dim RightNow, Daysleft, Hoursleft, Minutesleft
RightNow = Now returns the current Date and time.
Daysleft = Int (DateSerial (RightNow) _
+ 1, 1, 1)-RightNow
Hoursleft = 24-hour (RightNow)
Minutesleft = 60-minute (RightNow)
Print Daysleft & "Days left in the year."
Print Hoursleft & "hours left in". "
Print Minutesleft & "minutes left in the hour."
End Sub
You can also perform an operation on the Date/time value. Increase or decrease the number of days by adding or reducing an integer, or by adding or reducing a fraction to increase or decrease the time. So, plus 20 is plus 20 days, and minus 1/24 is minus one hour.
The valid range of Date values stored in variant variables is from January 1, 0100 A.D. to December 31, 9999. Date is calculated without regard to the dates before the Gregorian calendar was adopted. So if the calculation of the Gregorian calendar was adopted that year (in the United Kingdom and its colonies for 1752 years, or some other country, or earlier or later), the result might be incorrect.
You can use Date/time text in your code as long as you surround them with a pair of "#" numbers, just as you enclose a pair of double quotes ("") for a string. For example, you can compare a Variant that contains a Date/time value with a string of text that represents Date:
If somedate > #3/6/93# Then
The following example compares a Variant variable that contains a Date/time value to a string of text that represents Date and time:
If somedate > #3/6/93 1:20pm# Then
If the Date/time value does not include time, Visual Basic automatically sets the date of the value to midnight (the beginning of the day). If the Date/time value does not include date, Visual basic automatically sets the date portion of the value to ad December 30, 1899.
Visual Basic accepts Date and time formats for many types of text. The following Date/time values are all valid:
Somedate = #3-6-93 13:20#
Somedate = #March, 1993 1:20am#
Somedate = #Apr -2-93#
Somedate = #4 April 1993#
For more information about handling Date in an international format, see Chapter 16th, "Internationalization." You can use the IsNumeric function to test whether a Variant variable contains a value, which can be considered a valid value, and the IsDate function to test whether a Variant variable contains a value that could be considered a valid Date/time value.
The value can then be converted to a Date/time value using the CDate function. For example, the following code tests the Text property of the textbox with IsDate. If the property contains text that is considered a valid date, Visual Basic converts the text to date and calculates a few days before the end of the year.
Dim Somedate, Daysleft
If IsDate (Text1.Text) Then
Somedate = CDate (Text1.Text)
Daysleft = DateSerial (Year (somedate) + _
1, 1, 1)-somedate
Text2.text = Daysleft & "Days left in the year."
Else
MsgBox Text1.Text & "is not a valid date."
End If
For more information about the various date and time functions, see the "Date function" in the Visual Basic 6.0 Language Reference manual.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.