In the 85. In the variant of basic and LotusScript, I mentioned variant variants in basic-style languages. Variant types are often used in lotusscript for a variety of reasons.
1. The return type of a function cannot be declared as an array, and can only be used with variant types if required.
2. Methods for customizing objects do not support overloading, and you need to pass in multiple types of parameters only with variant type.
3. Array variables cannot be assigned as a whole, such as from Split () or Doc. ItemName, can only be used in variant types.
4. You need to write logic that is common to multiple data types.
LotusScript is a type system that takes a class definition (class definition, duck type duck typing with a language such as JavaScript), performs compile-time type checking when no variant is used, static type checking type-checking), and once the variant type is used, the type check is deferred to the runtime, dynamic type checking (type-checking). The two types of prosecutors are superior to the other. However, the variant type has many differences and special problems when it is used compared with the normal data type, which deserves special mention.
Assign value
The variables are assigned in LotusScript, according to the data types of variables, divided into several cases.
Scalar: Includes a variety of numeric types, strings, dates and other single values, with let statements assigned values, let is usually omitted.
Objects: Include product objects (types of objects such as notesdocument in notes), custom objects, and OLE objects, and set cannot be omitted by assigning values to set statements.
Arrays and lists: cannot be assigned as a whole, only a single element can be assigned a value. Whether to use set is determined by the data type of the array or list element.
Variant type: Determined by the specific data type of the assigned value, and set if the object.
User-defined data type (user-defined types): As with scalars, let statements are assigned values, and let is usually omitted. However, user-defined data type values cannot be assigned variant variables.
It is visible from the above, because variant variables can accommodate both scalar and object, so whether the assignment is set to be based on the specific data type of the assigned value, and if the assigned value itself is a variant, if the object is not known at compile time, an error may occur at run time. Must add set without times error: Set required on class instance assignment. Do not add set plus times wrong typemismatch.
Therefore, before assigning a Variant value to a VARIANT type, 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 S Ub
Object Type
In a completely object-oriented language like Java, judging whether an object is a type has a specialized operator instanceof. LotusScript also has a similar operator Isa, but it has some limitations. If you define an object type in a script library lib1 MyClass, a function foo defined in another script library LIB2 uses ISA, and then references the two script libraries in a proxy, declares a variable to be the MyClass type, and then passes that variable to foo. The result of the ISA operation was unexpectedly false. The reason is that ISA can only judge the type of object it is in, the MyClass is not defined in Lib2, and Lib2 does not refer to LIB1, so MyClass is unknown to it. The workaround is to use the TypeName function, which accurately obtains the type name of the custom object, regardless of whether the type of object it is testing is known in the scripting environment in which it is run. So we can write a complete version of the following ISA:
Function instanceof (v as Variant, className as String) as booleanif not IsObject (v) Theninstanceof=falseelsedim DT as Inte Gerdt=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 of
The equality of variables in a program (equality) can be divided into values equal (value equality) and reference Equality (reference equality). A single value is only necessary to determine whether the value is equal, and there is no difference between two 3. Composite values (containers such as arrays and objects) are not only expensive to compare the values of all members, but are often not possible because of private fields. There are two solutions, one is to compare the reference of the object is equal to the address, that is, any two object variables only point to the same object instance when it is considered equal. Another approach is to overload the Equals method of object as necessary to provide specific criteria for judging equality, as is the case with objects in Java. In Java, for example, the = = operator compares values for equality when used on a single value, and compares references for equality when used with objects.
Back to LotusScript, the data type of the variable is also divided into several major categories. The = operator is used to calculate the equality of single values, and the IS operator is used to calculate the equality of objects. Arrays and lists cannot be compared at all, and which operators are not allowed (Type mismatch). So what if we want to compare two variants that can accommodate a variety of data types? It is 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>& Lt;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 n Umbers and Boundsif not arrayboundsequals (v1, v2) thenequals=falseexit functionend If ' Change the arrays to one Dimensiondi M A1 as Variant, A2 as Varianta1=arraytoonedimension (v1) a2=arraytoonedimension (v2) Dim I as Integerfor I=lbound (A1) to Ubou nd (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 IsEle ment (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 are > 8192 ' Fixed arrayresult=8192case is > 2048 ' Listresult=2048end Select datatype4equals=resultend Function
The above two functions together can compare the equality of any two variant types. For single values, compare values for equality. An array and a list that compare the equality of each element in turn. For an object, if the object of that type defines the Isequalto method, the method is called, otherwise the reference equality is compared. Null, empty comparison has been overridden. Conversions between numerical types with different precision have also been considered.
87. Re-talk about variant variant