The variant in basic and LotusScript

Source: Internet
Author: User
Tags ole

Origin

I recently went back to the notes that I had written about notes, and found that in the Notes Bugs category, the two numbered intervals were roughly the same.

2. ' Type mismatch ' occurs if an nested the array e.g. when an item of columnvalues was an array, was assigned to a variant or PA Ssed as an argument of a function.

4. When an item of columnvalues was an array, ' Type mismatch ' occurs if it's assigned to a variant, E.g.columns=entry. Columnvalues

That means at least two things. One is when I first encountered the above errors, recorded, did not pay attention to avoid, and even completely forget, the next time you hit, but also as a new discovery record. Second, my English is kept on the same level, and the style of the same thing is basically consistent.

I think it is necessary to say something about this lesson that fell two times in the same place. Can remind you not to repeat my mistakes, but also to deepen my own impression, lest next time to remember a duplicate notes.

The source of the variant in LotusScript

In order to fully understand this problem and to gather more words, we need to start with the variant (variant type). The variant type in Visual basic is a data structure that accommodates a variety of types, "Note 1". Technically, it is taggedunion (tagged union), occupies 16 bytes of memory, the first two bytes hold the encoding of the data type (i.e. the label), the third to eighth byte is blank for alignment, and the Nineth to 16th total is eight bytes to save the actual data (that is, the Union). The first two bytes of content can be returned with the VarType function as follows:

Constant

Value

Description

Vbempty

0

Empty (uninitialized)

Vbnull

1

Null (no valid data)

Vbinteger

2

Integer

Vblong

3

Long integer

Vbsingle

4

Single-precision floating-point number

Vbdouble

5

Double-precision floating-point number

Vbcurrency

6

Currency value

Vbdate

7

Date value

Vbstring

8

String

Vbobject

9

Object

Vberror

10

Error value

Vbboolean

11

Boolean value

Vbvariant

12

Variant (used only with arrays of variants)

vbDataObject

13

A data Access Object

Vbdecimal

14

Decimal value

Vbbyte

17

Byte value

Vblonglong

20

Longlong integer (Valid on 64-bit platforms only.)

Vbuserdefinedtype

36

Variants that contain user-defined types

VBArray

8192

Array

Because the variant type can contain various data types, it immediately brings both positive and negative effects. The disadvantage is that compile-time type checking is canceled and errors are easily introduced. The advantage is that there is no static type of bondage, you can write a more flexible code "Note 2".

Usefulness and characteristics

LotusScript is a basic-style scripting language that inherits the features of basic when it was invented, including variant types. Variants in LotusScript can contain all data types except user-defined types. The return value for the datatype function that determines the specific data type is as follows:

Return

Value type

Constant

Variants only

0

EMPTY

V_empty

Yes

1

Null

V_null

Yes

2

Integer

V_integer

3

Long

V_long

4

Single

V_single

5

Double

V_double

6

Currency

V_currency

7

Date/time

V_date

Yes

8

String

V_string

9

OLE object or Nothing

V_dispatch

Yes

10

OLE Error

V_error

Yes

11

Boolean

V_boolean

12

Variant List or array

V_variant

13

IUNKNOWN (OLE value)

V_iunknown

Yes

17

Byte

V_byte

34

User-defined Object

V_lsobj

35

Product Object

V_prodobj

2048

List

8192

Fixed Array

8704

Dynamic Array

For the last three items in the table as containers, the return value of datatype is the value of their corresponding values plus the type values of the elements they contain. For example, a fixed array of strings, 8192+8=8200, and 8704+12=8716 for variant dynamic arrays. Note that the Variant type value 12 is used only in variant lists and arrays, because when you apply datatype to a single variant, you get the value of the specific data type that it contains.

I prefer the certainty of static type variables and compile-time type checking to ensure a sense of security, so unless a variant is not preferred in special cases. But the grammatical limitations of lotusscript make it possible to use them in some situations.

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.

When assigning an array value to a variant, it does not point to the original array, and a new array is copied, so the data changes from the original array cannot be read from it.

Dim logger As Log4domset Logger=getlogger (Nothing) Dim VDim fa (0) as Stringv=falogger. Printmsg (V (0)) ' [1]14:56:03:63-fa (0) = "fa"              ' [1]14:56:07:67-logger. Printmsg (V (0))
It is visible from above when the contents of the original array FA have changed, the Variant v contains an array that is not affected.

Problem

With the variant type Although the above is said to be non-tortuous, but also have rules to follow, can understand. Here's what I mentioned at the beginning of the article that neither document nor I can understand.

Finding NotesDocument or notesviewentry through a view, and then reading related data from them, is the most common scenario in programming. If you are reading a field or calculated value that is displayed by the view, the column value of the view is also indexed (84. The Notes database (bottom), which is said from the view index, is faster than reading through the document. For example:

Dim VDim fa (0) as Stringv=fafa (0) = "fa" Dim da () as Stringredim da (0) da (0) = "Da" Dim container (2) as Variant ' V=container ' No E Rror ' print ' DataType (container): "& DataType (Container) ' 8204 ' print" DataType (v): "& DataType (v) ' 8716 ' Container (0) =1 ' v=container ' no error ' Set container (0) =new notessession ' v=container ' no error ' container (0) =fa ' Container (1) =da ' V=container ' Error

When assigning a Variant array to a Variant variable from the above, it is fine if the elements of the type array are empty, number, string these scalar values, or object values such as NotesDocument. If the element itself is an array, an error occurs. In other words, a Variant variable cannot hold two layers or more of nested arrays. Whether this is inherited from basic, why, I do not know now. So when it comes to using columnvalues and multivalued columns, you can't assign it to a Variant variable, and you can only keep writing it.

ForAll e in entry. Columnvalues (1) Print Eend forall

Note 1: Other data types can be accommodated in addition to fixed-length strings and user-defined types before VB6. The variant of VB6 can also accommodate user-defined types.

Note 2: In VB, because you often interact with OLE objects, such as the collection type and the IDispatch interface involve objects that are not determined by the compile-time type, you must use variant types. VB's optional (optional) function parameters are defined as variant types because they may be omitted at invocation and have no content. In addition, method calls to variant objects are late-bound and can be used to write more generic code.

The variant in basic and LotusScript

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.