C_VARIANT to store different type of values

Automatically translated from English

Sometimes you want to write code being able to work with different variable types, this is especially true for generic coding. Using pointers can make code more difficult to read and debug. Sometimes it’s not possible at all, for example when you want to assign a generic variable to a form object as the data source.

A new command is now at your disposal to let you to declare a variable that can store different types of values. Say hello to the new C_VARIANT command!

Test the type of your variant

The variant data type can be used to represent other data types (except arrays). This means that the type of the variable is variant but the type of value can be Text, Longint or another valid type. You can use the Type command to get the type of the variable and the Value type command for the type of the value:

C_VARIANT($variant)
$t:=Type($variant) // type: variant
$vt:=Value type($variant) // value type: undefined
 
$variant:=6.5
$t:=Type($variant) // type: variant
$vt:=Value type($variant) // value type: real
 
$variant:="Hello world"
$t:=Type($variant) // type: variant
$vt:=Value type($variant) // value type: text

variant as a method parameter

There’s also times when you may need to create methods that accept parameters with several types. Now there’s no more need of pointers, you can create methods that accept the variant data type as a parameter:

C_COLLECTION($1)
C_VARIANT($2)
 
if($2#Null)
  
$1.push($2)
end if

And, of course, you can also create methods that return variant type:

C_TEXT($1;$JSFunction)
C_VARIANT($0;$Result)
 
$JSFunction:=$1
 
WA EXECUTE JAVASCRIPT FUNCTION(MyWArea;$JSFunction;$Result)
 
$0:=$Result

The variant data type can be useful in many ways, particularly when you need to use Null or Undefined values. Find more details on the doc center.

Fabrice Mainguené

• Product Owner •

Fabrice Mainguené joined 4D Program team in November, 2016. As a Product Owner, he is in charge of writing the user stories then translating it to functional specifications. His role is also to make sure that the feature implementation delivered is meeting the customer need.

After obtaining a Bachelor degree in Computer Science at CNAM, Fabrice joined a small software publishing company as a Windev developer. Then he worked for different companies in industry and trade areas as a Windev and web developer as well as technical advisor on new features.