Questions tagged [variant]

A variant data type is a tagged union that holds other data types. It is a standard data type in [ocaml], and typically used for interop calls between languages ([c++] and [vb6]) in classic Microsoft Windows [com] programming. It also exists in other languages using other names, such as [discriminated-unions] or the more general concept of [algebraic-data-types]

In , the variant type is a basic building block of the language. Custom variants are defined with a set of constructors that restrict which types it can hold, and are used to inspect it in and to warn if a pattern match does not cover all constructors.

Questions that should have this tag would include ones involving how to pass data between Windows processes or within the same process using and classic function calls. Often the calls occur between languages (e.g. to ), but they are also used in single language programs (e.g. ) to achieve a plug-in, modular architecture with DLL files.

A variant is technically a structure holding a union of data types along with a VARTYPE that indicates which member of the structure is valid. It can hold any other data type, including primitive types (BYTE, LONG), pointers to primitive types (LONG*, FLOAT*), IDispatch pointers, and even pointers to other variants.

There are a couple of helper classes Microsoft has created to help with the details of dealing with raw variants:

These classes relieve much of the grunt work of interop programming with variants such as doing deep destroys on contained SAFEARRAYs.

Definitions

  • Tagged union: A data structure that can be used to hold a number of different data types, only one of which is valid at a time. There is a field (VARTYPE) which indicates the valid data member (https://en.wikipedia.org/wiki/Tagged_union)

Examples

OCaml

(* Definition of the built-in option type *)
type 'a option =
| Some of 'a
| None

(* Construction *)
let var = Some "hello"

(* Inspection through pattern matching *)
let greeting =
  match var with
  | Some str -> str
  | None -> "howdy"

Visual Basic

Dim var as Variant

var = "hello"   'Variant holds a string

var = 7         'Variant holds an Integer

C++

_variant_t var;

var = "hello";  //Variant holds a string

var = 7;        //Variant holds an int

Resources

Related tags

1152 questions
-1
votes
1 answer

copy variant to char*

how to copy to a character array from variant in c++. So far I have been able to come up with the snippet below. but need help in making it work. _variant_t vt_Data(parent->CharData); if(vt_Data.vt != VT_NULL) { long ubound; long lbound; …
TrustyCoder
  • 4,749
  • 10
  • 66
  • 119
-2
votes
2 answers

VBA: (a) Is there a different VarType() in .Net than in Office? (b) What is vbChar?

In the Microsoft documentation for Office VBA, the page on the function VarType() lists its return values. And there is a page with a list of "VarType constants" without any explanation of what they are or where they are used, but the list is the…
NewSites
  • 1,402
  • 2
  • 11
  • 26
-2
votes
1 answer

C# Dynamic vs. Variant

According to this article: it is important to remember that C# is a statically typed language; which leaves no room for variant types Which seems correct... However doesn't the Dynamic data type break that rule? Maybe I am missing some subtle…
Anthony Griggs
  • 1,469
  • 2
  • 17
  • 39
-2
votes
1 answer

Change the product variant by clicking on the variant image Woocommerce

I have shop created in wordpress with woocommerce. I have a question. How i can do when i want change the product variant by clicking on the variant image ? When i select product variant in dropdown, image changes. But when i click on variant image,…
-2
votes
5 answers

De facto list of primitive types usable in C++

If, for example, you're going to write a variant type class, you will naturally need identification of what type an instance of that class is carrying. I'm wondering if anyone knows of any official or semi-official (de-facto?) reference of all…
sharkin
  • 12,162
  • 24
  • 86
  • 122
-2
votes
2 answers

In C++ how does one multiply an iterator of a vector which contains std::variant elements? Doing a cast of the iterator type?

#include #include #include #include struct S1 { int a {}; }; struct S2 { std::string b {}; }; using Struct_variant = std::variant< S1, S2 >; int main() { std::vector struct_vector { S1…
Grant Rostig
  • 473
  • 1
  • 4
  • 13
-2
votes
1 answer

How to directly access to what's in VARIANT variables in C++?

My program uses an external ocx library and receives data through it. The code below shows how it works. VARIANT varArrItem, varArrData; ocx_instance.GetItemArr(real, &varArrItem); // the library provides GetItemArr …
maynull
  • 1,936
  • 4
  • 26
  • 46
-2
votes
2 answers

Variant Array multiple errors and failure

People of the internet, I need your help! I am trying to use variant arrays to summarise a large dataset of performance data into individual scores. I have a table with about 13000 rows and about 1500 employees to loop through. I am not new to VBA…
-2
votes
1 answer

Perl : How to match data in entire text

In the following code I am trying to use data from Input File 1 to edit data in Input File 2. But the problem is the code is not able to match or substitute when the possible match text is anywhere other than at the last, towards right. Could you…
Tanumoy
  • 3
  • 4
-2
votes
2 answers

olevariant to an array of object

Im trying to get a olevariant 'array of object' I have this c++ code that do the job but i failed to traduce it to delphi code VARIANT vComps; HRESULT hr = swAssembly->GetComponents(VARIANT_TRUE, &vComps); IDispatch* HUGEP *pDispData; HRESULT hr =…
Tokazio
  • 516
  • 2
  • 18
-4
votes
1 answer

Why is this wrong

I have the following code procedure TfrmJsApplications.colMaintStylesGetContentStyle( Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem; out AStyle: TcxStyle); var aColumn: TcxCustomGridTableItem; …
OZ8HP
  • 1,443
  • 4
  • 31
  • 61
1 2 3
76
77