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

Method of a ATL/COM object : calling it in VBA for excel versus calling it in c#

I created a ATL/COM class, with a method (IDL) : [id(4)] HRESULT MULT3(double lCoeff, [in, out] VARIANT* pVarInOut); It takes pVarInOut, checks that it is an array whose coefficients are real numbers, and updates all its coefficients by multiplying…
Olórin
  • 3,367
  • 2
  • 22
  • 42
0
votes
1 answer

Qpid Variant type to handle byte arrays c++

I'm working on a project and I need to be able to send byte arrays through Qpid, but Variant has no idea what an array is. Is there some way, I can convert the byte array into something Variant, like maybe a Variant::list of unit8, or a…
Teddy Black
  • 193
  • 2
  • 14
0
votes
1 answer

How to bind a variant apply_visitor?

I've got this (C++03) code, but somehow, bind refuses to work. Any ideas why? typedef boost::variant Container; std::vector v; ... class IsBad: public boost::static_visitor<> { public: typedef bool result_type; …
Sam
  • 19,708
  • 4
  • 59
  • 82
0
votes
2 answers

Is it possible to create an error and assign it to an error subtype variable in VBScript?

Theoretically, if VarType(foo) returns 10 or vbError, it means that foo is an error. Am I wrong? So how do I force this to happen? Is it even possible to force foo to hold an error? If it's not, then I don't understand why Error is a variable…
Enorma
  • 3
  • 2
0
votes
1 answer

Returning Simple Array from VBA function throws Error When assigned to Variant or Another Array

Following is my function : Function FindMin(MinArray() As Integer) As Integer() Dim Min As Integer Dim Index As Integer Dim ReturnArray(2) As Integer Dim i As Integer Min = MinArray(0) Index = 1 'MsgBox UBound(MinArray, 1) - 1 For i = 1 To…
0
votes
1 answer

using string methods on variant vbscript

I am currently having a variant variable with string vartype. That specific variable may contain string such as "005" and I would like to change it to "5" (without converting it to a number), if it was a regular string I would have done it like…
Shperb
  • 474
  • 7
  • 19
0
votes
1 answer

Excel VBA worksheetfunction.transpose returns dimensions but no values

This code is originally much longer and intends to use Savitsky-golay fitting to take derivative estimates. In order to take find the derivative matrix needed to fit against original data, I need to follow through a series of excel worksheet…
0
votes
2 answers

VBA Pass User selected 2d Range vs static array in place of Arr

I have a need to convert the following function to accept a user selected 2 dimensional range [datatype] in place of Arr [ the arbitrary custom defined Array] that is used in it's place. I want to do this so I can run worksheet functions on the…
thistleknot
  • 1,098
  • 16
  • 38
0
votes
0 answers

VBA Creating a worksheet to store data for further range manipulation

I would like to import a range as a variant with some slight changes to what is stored in the variant, and then export the [modified] variant to a worksheet as a new range, that is then reread back in as a new range where I can do a new set of…
thistleknot
  • 1,098
  • 16
  • 38
0
votes
2 answers

Delphi 2010 variant to unicode problem

I am working on a DLL in Delphi 2010. It exports a procedure that receives an array of variants. I want to be able to take one of these variants, and convert it into a string, but I keep getting ????? I cannot change the input variable - it HAS to…
Crudler
  • 2,194
  • 3
  • 30
  • 57
0
votes
1 answer

Correct way to create CMap of VARIANTs

I need to create a dictionary collection of VARIANTs using CMap class. My dictionary is defined as such: CMap map; I then add elements as such: void setKeyValue(CMap
c00000fd
  • 20,994
  • 29
  • 177
  • 400
0
votes
1 answer

How to get array data with CATIA using com4j API to get point coordinate?

I try to retreive information from catia usig com4j. Some methods require to pass an array in argument to retreive information but the array is never populated. In this example is to get coordinate from a point in catia. The declaration of the…
tao
  • 3
  • 3
0
votes
1 answer

About _variant_t type in a MFC Library

inside my MFC (VC2010 SP1) project, I'm widely using a third party library to write some data in a database. This library is quite old (I've found it compiled for VS2005) and uses _variant_t to handle data. In a particular case anyway I get a…
IssamTP
  • 2,408
  • 1
  • 25
  • 48
0
votes
1 answer

OCaml specific Hashtbl type

This is probably a silly question, but I want to define a type like this: type bla = Bla of (string, bla) Hashtbl However, it gives me a parse error, no matter what I do. In essence, I want Bla to hold hashtable from string to bla again. What am I…
zpavlinovic
  • 1,507
  • 1
  • 17
  • 36
0
votes
2 answers

Calling c++ code from C# - Values not altered as expected

I'm working on converting one of our systems from C++ to c#. We're still using a lot of our C++ libraries in the new system. I'm pretty new to C++ and interop stuff but until now I've managed to get by. One particular piece of code has been…
OnABauer
  • 609
  • 4
  • 18