4D Blog

Home Product C_VARIANT to store different type of values

C_VARIANT to store different type of values

November 14, 2019

Product

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.

Discuss

Tags Language, Programming, v18

Latest related posts

  • February 3, 2026

    4D Write Pro – Adding a margin automatically when bullets are set using standard actions

  • January 22, 2026

    Transform Static Documents into Actionable Knowledge with AIKit

  • January 22, 2026

    Deploy Fluent UI effortlessly in your 4D applications

Fabrice Mainguené
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.
  • Deutsch
  • Français
  • English
  • Português
  • Čeština
  • Español
  • Italiano
  • 日本語

Categories

Browse categories

  • AI
  • 4D View Pro
  • 4D Write Pro
  • 4D for Mobile
  • Email
  • Development Mode
  • 4D Language
  • ORDA
  • User Interface / GUI
  • Qodly Studio
  • Server
  • Maintenance
  • Deployment
  • 4D Tutorials
  • Generic
  • 4D Summit sessions and other online videos

Tags

4D AIKit 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R10 21 21 R2 Administration AI Artificial Intelligence Build application CI/CD Class Client/Server Code editor Collections Formula Listbox Logs Mail Microsoft 365 Network Objects OpenAI ORDA PDF Pictures Preemptive Programming REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience vscode Web Word processor

Tags

4D AIKit 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R10 21 21 R2 Administration AI Artificial Intelligence Build application CI/CD Class Client/Server Code editor Collections Formula Listbox Logs Mail Microsoft 365 Network Objects OpenAI ORDA PDF Pictures Preemptive Programming REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience vscode Web Word processor
Subscribe to 4D Newsletter

© 2026 4D SAS - All rights reserved
Terms & Conditions | Legal Notices | Data Policy | Cookie Policy | Contact us | Write for us


Subscribe to 4D Newsletter

* Your privacy is very important to us. Please click here to view our Policy

Contact us

Got a question, suggestion or just want to get in touch with the 4D bloggers? Drop us a line!

* Your privacy is very important to us. Please click here to view our Policy