87. Let's talk about Variant and 87 variant.

Source: Internet
Author: User

87. Let's talk about Variant and 87 variant.
In the article Variant in 85. BASIC and LotusScript, I mentioned Variant in BASIC-style languages. Ing is often used in Lotus script for the following reasons.

1. The return type of the function cannot be declared as an array. If this is required, only the variant type can be used.

2. The methods of custom objects do not support overloading. You can only use the variant type when multiple types of parameters are required.

3. the array variable cannot be assigned as a whole, for example, from Split () or doc. ItemName, it can only be changed.

4. You need to write the logic that is common to multiple data types.

LotusScript is a type system that uses class definitions (opposite to the duck Type duck typing in JavaScript and other languages). If you do not use a variant, perform a type check during compilation, static type-checking is used. Once a variant is used, the type check is delayed until running, that is, dynamic type-checking ). The two types of procuratorates are superior to inferior. However, there are many differences and special problems between the application of the variant type and the common data type, which deserves special attention.

Assignment

Variables are assigned a value in LotusScript, which is divided into several situations based on the data type of the variables.

Scalar: it includes various numeric types, strings, dates, and other single values. It is assigned with a Let Statement, which is usually omitted.

Objects: including product objects (such as various objects of NotesDocument in Notes), custom objects, and OLE objects. Use the Set statement to assign values. Set cannot be omitted.

Array and list: An array and list can only be assigned values to a single element. Whether to use Set depends on the Data Type of the array or list element.

Variant: it is determined by the specific data type assigned. If it is an object, add Set.

User-defined data types: Like a scalar, Let statements are used to assign values, which are usually omitted. However, the user-defined data type value cannot be assigned a variant variable.

It can be seen from the above that, because a variant variable can accommodate both scalar and object, whether to add Set during the assignment depends on the specific data type assigned, if the assigned value is a variant and the object is unknown during compilation, an error may occur during the runtime. Set required on class instance assignment must be added. If Set is not added, Typemismatch is incorrect.

Therefore, before assigning a variant value to a variant, you must explicitly determine whether the assigned value is an object.

Sub SetValue(variable As Variant, value As Variant)If IsObject(value) ThenSet variable=valueElsevariable=valueEnd IfEnd Sub
Object Type

In a fully object-oriented language such as Java, there is a special operator instanceof to determine whether an object is of a certain type. Lotus script also has a similar operator IsA, but it has some limitations. If you define an object type MyClass in lib1 of a script library, use IsA for a function Foo defined in lib2 of another script library, and then reference these two script libraries in a proxy, declare a variable of the MyClass type and upload it to Foo. The result of the IsA operation is unexpectedly False. The reason is that IsA can only determine the object type known to its script environment. MyClass is not defined in lib2, and lib2 does not reference lib1. Therefore, MyClass is unknown to it. The solution is to use the TypeName function, regardless of whether the object type to be tested is known in the script environment it runs, you can accurately obtain the type name of the custom object. Therefore, we can write the following complete IsA version:

Function InstanceOf(v As Variant, className As String) As BooleanIf Not IsObject(v) ThenInstanceOf=FalseElseDim dt As Integerdt=DataType(v)If dt=V_LSOBJ Or dt=V_PRODOBJ ThenIf TypeName(v)=UCase(className) ThenInstanceOf=TrueElseInstanceOf=FalseEnd IfElseIf v IsA className ThenInstanceOf=TrueElseInstanceOf=False End IfEnd IfEnd IfEnd Function

Equality

The equality of variables in the program can be divided into value equality and reference equality ). A single value is only required to determine whether the values are equal. There is no difference between the two three. The composite values (containers and objects such as arrays) must compare whether the values of all Members are equal, which is not only costly, but also impossible because of private fields. There are two solutions: one is to simply compare the object reference, that is, whether the address is equal, that is, any two object variables are considered equal only when they point to the same object instance. Another way is to overload the equals method of an Object as necessary as an Object in Java, providing specific criteria for determining equality. Java is used as an example. The = Operator is used in a single value, and the comparison value is equal. When used in an object, the comparison reference is equal.

Back to LotusScript, the Data Types of variables are also divided into several categories. = Operator Is used to calculate the equality of a single value. Is operator Is used to calculate the equality of objects. Arrays and lists cannot be compared as a whole. Type mismatch is not allowed ). So what if we want to compare two variants that can accommodate various data types? It can only be handled separately in various situations:

Public Function Equals(v1 As Variant, v2 As Variant) As Boolean'Check data typeDim type1 As Integer, type2 As Integer'Type conversion for numericals, lists and arrays of variantstype1=DataType4Equals(v1)type2=DataType4Equals(v2)If type1><type2 ThenEquals=FalseExit FunctionEnd If'Empty or NullIf type1=V_EMPTY Or type1= V_NULL Then Equals=TrueExit Function End If'ScalarIf IsScalar(v1) ThenIf v1=v2 ThenEquals=TrueElseEquals=False End IfExit Function End If'ObjectIf IsObject(v1) ThenIf v1 Is v2 ThenEquals=TrueElseOn Error ErrNamedMemberNonExist GoTo NotEqualsIf v1.IsEqualTo(v2) ThenEquals=TrueElseEquals=False End IfEnd IfExit Function End If'ArrayIf IsArray(v1) Then'Check dimension numbers and boundsIf Not ArrayBoundsEquals(v1, v2) ThenEquals=FalseExit FunctionEnd If'Change the arrays to one dimensionDim a1 As Variant, a2 As Varianta1=ArrayToOneDimension(v1)a2=ArrayToOneDimension(v2)Dim i As IntegerFor i=LBound(a1) To UBound(a1)If Not Equals(a1(i), a2(i)) ThenEquals=FalseExit FunctionEnd IfNextEquals=True Exit FunctionEnd If'ListIf IsList(v1) ThenDim tag As StringForAll e In v1tag=ListTag(e)If Not IsElement(v2(tag)) ThenEquals=FalseExit FunctionElseIf Not Equals(e, v2(tag)) ThenEquals=FalseExit FunctionEnd IfEnd ForAllEquals=True Exit FunctionEnd IfNotEquals:Equals=FalseEnd FunctionPrivate Function DataType4Equals(v As Variant) As IntegerDim result As Integerresult=DataType(v) Select Case resultCase V_BYTE, V_INTEGER, V_LONG, V_SINGLE, V_DOUBLE, V_CURRENCYresult=V_CURRENCYCase Is > 8704 'Dynamic arrayresult=8704Case Is > 8192 'Fixed arrayresult=8192Case Is > 2048 'Listresult=2048End Select DataType4Equals=resultEnd Function

The above two functions can be combined to compare whether any two variants are equal. For a single value, the comparison value is equal. Compare the equality of each element in the array and list. If the issimilar to method is defined for an object of this type, this method is called. Otherwise, the equality of referenced objects is compared. The comparison of Null and Empty has been overwritten. Conversions between numeric types with different precision have also been considered.

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.