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

what is the smartest way to add all variants to product category page in shopify

I am looking at showing all Shopify product variants on the product collection page. We are using the basic theme. What is the easiest way to do this? I have looked at some examples but none of them are exactly what I need. I am not scared to…
-1
votes
1 answer

How to use a vector variants as key in unordered_map?

How can I use a vector of variants as the key in unordered_map? For example, I'd like to make the following code work. using VariantType = std::variant; using VecVariantType =…
dalibocai
  • 2,289
  • 5
  • 29
  • 45
-1
votes
1 answer

How to use variants as the key in unordered_map?

How can I use variants as the key in unordered_map? For example, I'd like to make the following code work. using VariantType = std::variant; std::unordered_map m; How do I…
dalibocai
  • 2,289
  • 5
  • 29
  • 45
-1
votes
1 answer

How can I insert multiple items of each data-type in a variant?

I can store 1 item of each data-type in a variant, like this: using var_t = variant; and then, like this: vector example1 = {1, 1.2f, 345l, 67.89, "S"}; But How can I insert multiple items of each data-type…
-1
votes
1 answer

VBA: Run through a 2D Variant Array deleting rows

I am importing a range as a 2D Variant into VBA and to declare an array as Variant by `Dim matrix(). I now want to loop through the rows and delete rows where the 2nd column = "even" or the 5th row ends with "_tom". Dataset below My main issue is…
Sam Harper
  • 61
  • 1
  • 3
  • 10
-1
votes
1 answer

How to dump Delphi OleVariant content to the file?

I have multi-tiered application that use OleVariant variables to send and receive data accross the line using custom communication framework. Usually the content of this OleVariant variable comes from TClientDataSet.Data (which, of course, is of…
TomR
  • 2,696
  • 6
  • 34
  • 87
-1
votes
1 answer

how to convert com instance to variant, to pass it in idispach invoke

i want to pass a com object instance as a variant parameter to another active x object function, for that i need to convert the idispatch pointer to a variant? i am not sure. hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); if…
Alok Saini
  • 321
  • 7
  • 18
-1
votes
2 answers

overload [] operator to return a variant type

EDIT: thanks to the answers I was able to solve all the issues with my code. I post here the solution: it might be useful to somebody in the future. In particular, the suggestion of using a proxy class proved very useful! The example doens't…
-1
votes
1 answer

How to pass variant* to C# through InvokeHelper?

I have a legacy c++ component.It consumes ATL Controls of similar nature.Prog ID is input to this component. It uses the progid to create ATL object and it uses dispatchid to call methods from ATL control using InvokeHelper as below, DISPID…
srajeshnkl
  • 883
  • 3
  • 16
  • 49
-1
votes
1 answer

Count with multiple variants

I have used Variants in the past for something similar, but it was one dimensional in its solution. I am wondering if utilizing Variants with two dimensions would be feasible. I have ever changing list of dates that correspond with a week, that…
A Cohen
  • 458
  • 7
  • 26
-1
votes
1 answer

convert variant into COleSafeArray in MFC c++

How can I put VARIANT into COleSafeArray? I tried Variant vara; COleSafeArray force= vara; and got "debug assertion failed" I used a library to convert voltage value to Force value. In Library, there is below function: VARIANT…
abcd
  • 11
  • 5
-1
votes
1 answer

why boost variant throw exception when template argument is non-POD?

For POD and even std::string boost variant works for me, but when I try my user type A, it fails for this code: #include "stdafx.h" #include #include struct A { char ch; }; int main() { …
rtischer8277
  • 496
  • 6
  • 27
-1
votes
1 answer

Sorting and subsorting a variant array

I would like to code the following in VBA, a function of signature Public Function MySort( v as Variant) as Variant that takes a variant v having names in its first columns and notes in its second, checks if it has two columns, throws an error if…
-1
votes
1 answer

Unassign OleVariant in Delphi 7

one of my OleVariant variable was unassigned 2 times by mistake in my program (under Delphi 7). Some end-users reported that the program may hang. I was wondering if the unassignment of an OleVariant (already unassigned) could be the cause of the…
user382591
  • 1,320
  • 5
  • 19
  • 39
-1
votes
1 answer

How to return a byte array as a variant in vb6

I've been trying to figure out why the function below throws an error saying "Type Mismatch" when it returns. From what I know about VB6, this should work without any issue, yet it obviously does not. Can anyone see what I am doing wrong here and…
Brandon
  • 890
  • 2
  • 13
  • 32
1 2 3
76
77