From 49937bb740e70e6a244f9f4e9a30b7541815e62e Mon Sep 17 00:00:00 2001 From: Rafael Lima Date: Tue, 27 Apr 2021 22:07:21 +0200 Subject: tdf#132325 Improve Variant/Any documentation This patch improves the help page "Using Variables": - Adds to the "Declaring Variables" section an explanation that variables of unspecified type will be considered as Variants - Creates a new section "The Variant type" to provide a brief overview of the Variant data type - Explains the Any data type Change-Id: Iaf076ad3ef22d24fb1aa4cfa089ed9d2a1bcf57c Reviewed-on: https://gerrit.libreoffice.org/c/help/+/114717 Tested-by: Jenkins Reviewed-by: Olivier Hallot --- source/text/sbasic/shared/01020100.xhp | 38 ++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/source/text/sbasic/shared/01020100.xhp b/source/text/sbasic/shared/01020100.xhp index 31124494e8..6866a7429b 100644 --- a/source/text/sbasic/shared/01020100.xhp +++ b/source/text/sbasic/shared/01020100.xhp @@ -80,6 +80,17 @@ Once you have declared a variable as a certain type, you cannot declare the variable under the same name again as a different type! +When you declare multiple variables in a single line of code you need to specify the type of each variable. If the type of a variable is not explicitly specified, then Basic will assume that the variable is of the Variant type. + + ' Both variables "a" and "b" are of the Integer type + Dim a As Integer, b As Integer + ' Variable "c" is a Variant and "d" is an Integer + Dim c, d As Integer + ' A variable can also be explicitly declared as a Variant + Dim e As Variant, f As Double + +The Variant type is a special data type that can store any kind of value. To learn more, refer to the section The Variant type below. +

Forcing Variable Declarations

To force declaration of variables, use the following command: @@ -192,6 +203,33 @@ dob = #2010-09-28# + + The Variant type + The Any type + +

The Variant type

+Variables declared as Variant can handle any data type. This means that the actual data type is defined during runtime as a value is assigned to the variable. +There are three main ways to create a Variant variable, as shown below: + + Dim varA ' The type is not specified, hence the variable is a Variant + Dim varB as Variant ' The variable is explicitly declared as a Variant + varC = "abc" ' Previously undeclared variables are treated as Variants + +The example below uses the TypeName function to show how the type of a Variant variable changes upon assignment. + + Dim myVar As Variant + MsgBox TypeName(myVar) ' Empty + myVar = "Hello!" + MsgBox TypeName(myVar) ' String + myVar = 10 + MsgBox TypeName(myVar) ' Integer + +A Variant variable is initialized with the Empty special data type. You can use the IsEmpty function to test if a variable is an Empty Variant. +You can also use the keyword Any to declare a variable as a Variant. In Basic, variables declared as Any are treated exactly as Variant variables. + + Dim myVar As Any ' Variable "myVar" is a Variant + +

Initial Variable Values

As soon as the variable has been declared, it is automatically set to the "Null" value. Note the following conventions: Numeric variables are automatically assigned the value "0" as soon as they are declared. -- cgit