KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Using the New C_VARIANT Command
PRODUCT: 4D | VERSION: 18 | PLATFORM: Mac & Win
Published On: January 24, 2020

4D v18 introduced a new variable type called "Variant". It is basically a one-size-fits-all type that allows encapsulating data of any valid fixed type in a variable. Variant is useful for writing generic code that returns or receives variables with types that can vary. With the new data type comes a new command that let's you declare a variable that can store different types of values: C_VARIANT. Here is a simple, generic example of how you could use it.

Suppose that you have a method, myMethod, that is called by multiple other methods, and that takes in one parameter. This parameter variable may be longint type, text type, or null type, depending on which method is calling myMethod. The action that myMethod takes depends on the type of the input variable. Thus, myMethod may be constructed like this:

// myMethod

C_VARIANT($1)

$input:=$1

$type:=Value type($input)

Case of
   : ($type=Is longint)
   // Do some action...
  
   : ($type=Is text)
   // Do some other action...
  
   : ($type=Is null)
   // Do another action...
End case


Note that the command Value type is used to get the actual type of the parameter that is passed in. If the command Type were used instead, it would return only the type variant.