Variant
set() [Variant class]
One common use-case of Variant is setting values in a ledger. In this code, a fake sensor value is stored in int sensorValue
. In a real application, you'd read an actual sensor there.
Then a Variant
object is created on the stack that will be filled with data.
The set()
method is described below in VariantMap. It takes a key name (such as "sensor" or "time") and a value, which is also a Variant
. Because there are Variant constructors for many variable types (see variant constructor, below), you can pass a variable directly to set and the compiler will handle it appropriately. In the time example, the returned object from Time.format
is a String
.
int sensorValue = readSensor();
Variant data;
data.set("sensor", sensorValue);
data.set("time", Time.format(TIME_FORMAT_ISO8601_FULL));
sensors.set(data);
See Ledger sensor for the full example.