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

Source: Internet
Author: User
Tags array arrays empty error handling expression integer variables valid

Object data type
The object variable is stored as a 32-bit (4-byte) address that references an object in the application or in some other application. A variable declared as object can then be specified (with the Set statement) to refer to any actual object recognized by the application.
Dim Objdb as Object
Set objdb = OpenDatabase ("C:\Vb5\Biblio.mdb")
When declaring an object variable, try a specific class instead of the generic object (such as using a TextBox without control, or replacing object with the Database as in the example above). Before running the application, Visual Basic can decide on the properties and methods that reference a particular type of object. As a result, the application is running at an even faster speed. A specific class is enumerated in the Object Browser.
When you use objects from other applications and enumerate objects in the classes list in the Object Browser, do not use Variant or generic object, but declare objects. This ensures that VisualBasic can identify the specific type of object referenced and solve the reference problem at run time.
For more information about creating and specifying objects and object variables, see the "Creating Objects" section later in this chapter.

converting data types
Visual Basic provides several transformation functions that you can use to convert a value to a specific data type. For example, use the CCur function to convert a value to a Currency type: Payperweek = CCur (Hours * hourlypay)

Note to the target data type, the value passed to the conversion function must be valid, otherwise an error occurs. For example, if you want to convert a long number to an integer number, the long number must be within the valid range of the integer data type.
Details find the specified conversion function in the online Help.

Variant data type
Variant variables can store data for all system-defined types. If you assign them to variant variables, you do not have to convert between the types of the data, and visual Basic automatically completes any necessary transformations. For example:
Dim somevalue ' defaults to Variant.
Somevalue = "Somevalue" contains "17" (two-character string).
Somevalue = SomeValue-15 ' Now, Somevalue contains a value of 2.
Somevalue = "U" & Somevalue ' Now, Somevalue contains "U2" (two-character string).
You can manipulate variant variables without paying much attention to the type of data in a Variant variable, but avoid falling into traps.
1. If you perform a mathematical or functional operation on a Variant variable, the variant must contain a number. For more information, see the section "Numeric values stored in variables" in the "Advanced variable Topics" in the Online Guide.
2. If you are connecting two strings, use the "&" operator instead of the "+" operator. For more information, see the "Strings stored in Variant variable" section of the online manual in advanced variable topics.
In addition to being able to operate like other standard data types, variants also contains three specific values: Empty,null and Error.

Empty value
Sometimes you need to know if you have assigned a value to the variable you created. The Variant variable has a value Empty before it is assigned. The value Empty is a specific value that is different from the 0, 0 length string ("") or Null value. Use the IsEmpty function to test the Empty value:
If IsEmpty (z) Then z = 0
When a Variant variable contains a Empty value, it can be used in an expression, and it is treated as a 0-or 0-length string, depending on the expression.
The Empty value disappears whenever any value, including a 0, 0-length string, or Null, is assigned to a Variant variable. The variant variable can be restored to Empty by assigning the keyword Empty to a Variant variable.

A

Null value
Variant data type can also contain a specific value: null. Null is typically used for database applications, representing unknown or missing data. Because null methods are used in the database, NULL has some unique attributes:
1. For an expression that contains null, the result is always null. So null is "propagated" by expression, and the value of the entire expression is null if the part of the expression is null.
2. Passing a null value, a NULL Variant variable, or an expression that evaluates to NULL as a parameter to most functions will return NULL to the function. The
3.Null value is propagated through intrinsic functions that return Variant data types. The
can also specify a null value with a null keyword.
Z = null
also use the IsNull function to test whether a Variant variable contains a Null value.
If IsNull (X) and IsNull (Y) Then
Z = Null
Else
z = 0
End If
If you assign a Null value to any other type variable other than the variant, you will receive a Error. Assigning a null value to a variant does not make an error, and null is propagated by an expression that contains a Variant variable (although NULL is not propagated through some functions). You can return Null from any function procedure that has a Variant return value.
unless you explicitly assign NULL to a variable, the variable is not set to a null value, so if you do not use NULL in your application, you do not have to write a program that tests null and handles NULL.
For more information about how to use NULL in an expression, see the "Null" section of the Language reference.

Error value
In a Variant, the error is a specific value that indicates the error status in the process that has occurred. However, unlike other types of errors, normal application-level error handling does not occur here. Therefore, the programmer or the application itself can make trade-offs based on the Error value. Error values can be created by converting real numbers into error values using the CVERR function. For more information about how to use Error values in expressions, see the "CVERR Functions" section in the Visual Basic 6.0 Language Reference manual. For information about error handling, see Chapter 13th, "Debugging code and handling errors." For more information about Variant data types, see "Advanced Variable Topics" in the Online Guide.

Advanced Variable Topics

The internal representation of a value in a variable
Variant variables maintain an internal representation of the values they store. This representation determines how VisualBasic handles these values when performing comparison operations and other operations. When you assign a value to a Variant variable, Visual Basic correctly records the value with the most compact representation. Future operations may cause visual Basic to change this representation for a particular variable (a Variant variable is not an untyped variable, but a variable that can arbitrarily change the type). These internal representations are consistent with the data types discussed earlier in this chapter in data types.
Note that the Variant always remains 16 bits, regardless of what data is stored in it. objects, strings, and arrays are not physically stored in a variant, in which case a four-byte variant is used to hold an object reference, or a pointer to a string or array. And the real data is somewhere else.
Often, you don't have to understand which variable type Visual Basic uses for a particular variable; VisualBasic automatically converts the type. You can use the VarType function if you want to know what type of variable is being used by Visual Basic.
For example, if you store a binary value in a Variant variable, Visual Basic is represented inside a Double. If you know that the application does not require the high precision (and hence the low speed) provided by the Double value, converting this value to single or even converting to Currency can speed up the calculation:
If VarType (x) = 5 Then X = CSng (x) is converted to single precision.
For an array variable, the value of the VarType equals the sum of the array elements and the return value of the data type. For example, this array contains a Double value:
Private Sub Form_Click ()
Dim Dblsample (2) as Double
MsgBox VarType (dblsample)
End Sub
Future versions of Visual Basic may add a new Variant representation, so any code that makes a decision based on the return value of the VarType function should be able to handle the not-currently-defined return value appropriately.
For more information about the VarType function, see "VarType function" in the Visual Basic 6.0 Language Reference manual. For more information about arrays, see "Arrays" later in this chapter. For more information about converting data types, see "Data Types" earlier in this chapter.

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.