value property

T get value

Gets or sets the Dart value represented by this VARIANT.

When reading, the current vt field determines how the value is decoded. The decoded value must be assignable to T.

Supported return types include:

When writing, the existing value is cleared before assigning the new one.

Supported input types include:

Implementation

T get value {
  final result = switch (vt) {
    VT_EMPTY || VT_NULL => null,
    VT_BOOL => boolVal,
    VT_BSTR => bstrVal.isNull ? null : bstrVal.toDartString(),
    VT_I1 => cVal,
    VT_I2 => iVal,
    VT_I4 => intVal,
    VT_I8 => llVal,
    VT_UI1 => bVal,
    VT_UI2 => uiVal,
    VT_UI4 => uintVal,
    VT_UI8 => ullVal,
    VT_R4 => fltVal,
    VT_R8 => dblVal,
    VT_DISPATCH => pdispVal,
    VT_UNKNOWN => punkVal,
    _ => this,
  };

  if (result is! T) {
    throw StateError(
      'Expected $T but got ${result?.runtimeType} for vt $vt.',
    );
  }

  return result;
}
set value (T value)

Implementation

set value(T value) {
  VariantClear(this);
  ZeroMemory(this, sizeOf<VARIANT>());

  if (value == null) {
    vt = VT_NULL;
    return;
  }

  switch (value) {
    case bool _:
      vt = VT_BOOL;
      boolVal = value;
    case double _:
      vt = VT_R4;
      dblVal = value;
    case IDispatch _:
      vt = VT_DISPATCH;
      pdispVal = value;
    case IUnknown _:
      vt = VT_UNKNOWN;
      punkVal = value;
    case int _:
      vt = VT_I4;
      intVal = value;
    case String _:
      vt = VT_BSTR;
      bstrVal = value.toBstr();
    default:
      throw ArgumentError.value(
        value,
        'value',
        'Unsupported VARIANT value type.',
      );
  }
}