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

Source: Internet
Author: User
Tags cos numeric numeric value rtrim trim

Numeric data type
Visual Basic supports several Numeric data types: Integer (integer), long, single (single-precision floating-point type), double (double-precision floating-point type), and Currency (currency type). The Numeric type occupies less storage space than the Variant type.
If you know that a variable always holds an integer (such as 12) instead of a number with a decimal point (such as 3.57), you should declare it to be an integer or a Long type. Integers are faster and have less memory than other data types. In the For ... Integer types are especially useful when used as counter variables within the Next loop.
For more information about control structures, see "Overview of Control Structures" later in this chapter. If the variable contains decimals, you can declare them as single, Double, or Currency variables. The Currency data type supports 4 digits to the right of the decimal point and 15 digits to the left of the decimal point; it is a precise fixed-point data type, applicable to currency calculations. The number of floating-point (single and Double) is much larger than the valid range of currency, but it is possible to produce small rounding errors.
Note that floating-point values can be represented as mmmeeee or mmmdeee, where MMM is the mantissa, and Eee is the exponent (a power of 10). The maximum positive value for a single data type is 3.402823E+38, or 3.4 times 10 of 38 times; The maximum positive value for the Double data type is 1.79769313486232d+308 or 1.8 times 10. Separating the number part and the exponent portion of a numeric literal with D causes the value to be treated as a Double data type. Similarly, using E in this manner also causes the value to be treated as a single data type.

Byte Data Type
If the variable contains a binary number, it is declared as an array of the Byte data type (the array is discussed in "arrays" later in this chapter). Storing binary data in a Byte variable during the conversion format preserves the data. When a String variable is converted between ANSI and Unicode formats, any binary data in the variable is corrupted. In either of the following cases, VisualBasic automatically converts between ANSI and Unicode:
When the file is read
When
calls the DLL when
calls methods and properties of the object
except for unary subtraction, All operators that can operate on integers can manipulate the Byte data type. Because Byte is a unsigned type from 0-255, it cannot represent a negative number. Therefore, when you make a unary subtraction operation, Visual Basic converts Byte to a signed integer first.
All numeric variables can be assigned to each other, or they can be assigned to variant type variables. Before you assign a floating-point number to an integer, Visual basic rounds the fractional part of the float, rather than removing the fractional part.
For more information about Unicode and ANSI transformations, see Chapter 16th, "Internationalization."

String data type
You can declare a variable to be of type string if it always contains a string and never contains a numeric value.
Private S as String
can then assign the string to the variable and manipulate it with a string function.
S = "Database"
S = Left (s,4)
By default, a string variable or parameter is a variable-length string. As new data is given to the string, its length can be increased and reduced. You can also declare that a string has a fixed length. A fixed-length string can be declared with the following syntax:
String * Size
For example, to declare a string of length 50 characters, you can use the following statement:
Dim empname As String
If the string is given fewer than 50 characters, the A space fills the empname of a piece of memory. If the length given to a string is too long to become a fixed-length string, Visual Basic truncates the characters that are out of proportion. 
Because fixed-length strings fill the trailing space with spaces, you can find that the Trim and RTrim functions for removing spaces are useful when working with fixed-length strings. A fixed-length string in a standard module can be declared public or Private. In forms and class modules, a fixed-length string must be declared Private.
For more information, see "Ltrim, RTrim, and trim functions" in the Visual Basic 6.0 Language Reference manual.

Swapping strings and numbers
If the string represents a numeric value, the string can be assigned a numeric variable. You can also assign a value to a string variable. For example, place command buttons, text boxes, and list boxes in a form. Enter the following code in the Click event of the command button. Run the application and click the command button.
Private Sub Command1_Click ()
Dim IntX as Integer
Dim Stry as String
Stry = "100.23"
IntX = Stry ' passes a string to a numeric variable.
List1.AddItem Cos (stry) ' Converts the cosine of a value in a string.
' Add to the list box.
stry = cos (stry) ' passes the cosine to a string variable.
Text1.Text = Stry ' Displays a string in the text box.
End Sub
Visual Basic automatically forces the variable to be the appropriate data type. Be careful when converting strings and values, and if the values in the delivery string are not numeric, an error occurs at run time.

Boolean data type
If the value of a variable is only "True/false", "yes/no", "On/off" information, you can declare it to be a Boolean type. The Boolean default value is False. In the following example, Blnrunning is a Boolean variable that stores a simple yes/no setting.
Dim blnrunning as Boolean
' See if the tape is spinning.
If recorder.direction = 1 Then
blnrunning = True
End If

Date data type
Date and time values can be included in a specific date data type and in a Variant variable. The general Date attribute applies to both types.
For more information, see the "Store date/time values in Variant variables" section in the advanced variable topics in the Online Guide.
When other numeric data types are converted to date, the value to the left of the decimal point represents the date information, and the value to the right of the decimal point represents time. Midnight is 0, noon is 0.5. A negative number represents Date before December 31, 1899 A.D.

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.