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
0
votes
1 answer

deal with boost variant and boost function

I would like a vector to hold differents kind of boost::function, so, I tried a variant : #include #include #include #include void a(int, int) {} void b() {} int main(int…
Titouan56
  • 6,932
  • 11
  • 37
  • 61
0
votes
1 answer

How to select Product and Variant in separate fields in Sales order?

In Odoo 8's product.py I see the name_search method concatenates the product name and variant as the product name. I however have hundreds of attributes to my product. Height: 500mm, 505mm,... 3000mm Width: 500mm, 505mm,... 4000mm So it makes it…
Herman Vercuiel
  • 69
  • 2
  • 13
0
votes
3 answers

Trying to convert an excel named range of strings (text with spaces) into a string array

Trying to convert an excel named range of strings (text with spaces) into a string array to use in a access database query. Basically, pull in list of names from excel, query database using those names, then return data related to those names back…
walter's human
  • 103
  • 1
  • 1
  • 9
0
votes
0 answers

Type mismatch when setting ADODB Recordset Date field. (VB6)

I have had reports of type mismatch errors in our program, and error logging points it to a line of code where we set a date field in a recordset to a date value stored in a variant, seen below: RS![DateField] = mDateVariant Unfortunately, whoever…
dingdangdowney
  • 501
  • 1
  • 8
  • 22
0
votes
1 answer

How to translate a C++ method with VARIANT to C# without VARIANT?

I've never worked with variants in C++. But I'm maintaining code from old C++ projects. Part of it is to translate it or part of it into C#. I found methods that use variants. I want to "kill" any variant use in a new project version. Can someone…
Bitterblue
  • 13,162
  • 17
  • 86
  • 124
0
votes
1 answer

Return a string from a C# method to a C++ function in an output argument

I am invoking a C# method from a C++ function. The C# method should do the following: Allocate a BSTR and return it to the C++ function in an output argument Return an HRESULT to the C++ function through the stack (by return-value) I have…
barak manos
  • 29,648
  • 10
  • 62
  • 114
0
votes
1 answer

C++ - Finding the proper design for this

I'm writing a script interpreter and i first need to tokenize a string containing the source code. For that i've identified different things : Identifiers (variable names) ; Symbols (+, -, etc... including "alphabetic" operators such as "return")…
Virus721
  • 8,061
  • 12
  • 67
  • 123
0
votes
1 answer

Problems when setting ranges to variants

EDITED to fix typo and add more details I think you can see what I want to do here: Dim bMatch As Boolean Dim vCriteria As Variant Dim rCell As Range Dim vCellArray() As Variant If rCell = "" Then 'Do nothing Else For Each…
user1283776
  • 19,640
  • 49
  • 136
  • 276
0
votes
1 answer

Casting between variant and bstr_t causing inconsisten crash in Windows 2008

We have a C# application, calling a simple C++ wrapper class, that then calls an existing C++ DLL. The C++ code is all VC++ 6.0. We are getting inconsistent behaviour, but the crash, when it happens, always happens within the C++ wrapper DLL, and…
meisen99
  • 576
  • 4
  • 16
0
votes
2 answers

LabVIEW - Variant to Database Variant

I've created a save VI that can save several different classes to database using dynamic dispatch. Each class has a different type defined cluster that I want to be saved. I'm trying to convert each cluster into a database variant, so they all…
kiml42
  • 638
  • 2
  • 11
  • 26
0
votes
1 answer

How to set variant specific variable in gradle?

I want to be able to define a variant specific variable. flavor1{ variable = "thistask" } flavor2{ variable = "thattask" } Then when i execute it like ./gradlew aFlavor1 i want to be able to read variable as "thistask". How can i accomplish…
guydemossyrock
  • 1,118
  • 9
  • 16
0
votes
1 answer

vcf-consensus script error: The sequence N not found in the fasta file

I am trying to use this script (vcf-consensus) with a simple example but I have one error: The sequence "7" not found in the fasta file. The syntaxis is: Usage: cat ref.fa | vcf-consensus [OPTIONS] in.vcf.gz > out.fa My FASTA file…
0
votes
2 answers

identifying the type

In my application, there is a inheritance hierarchy in which only the classes that are at the end of the inheritance chain are non-abstract classes. Also there is some usage of boost::variant. I want to write a function which takes a pointer and a…
Gokul
  • 893
  • 11
  • 27
0
votes
1 answer

Shopify , show product options as 3 seperate dropdowns instead of 1 single dropdown

On my product page, with the following line of code in the product.liquid template, it generates 3 seperate dropdowns for the 3 different product options the product has, eg Type Color Size..