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

Invalid Variant Operation when comparing an OleVariant to UnAssigned

This is my code: **if** FWordApp = UnAssigned **then** FWordApp := CreateOleObject('Word.Application') ; Result := FWordApp; The above sits in a GETter for a property of type OleVariant. The first time, it goes through fine, compares TRUE to…
Andrea Raimondi
  • 527
  • 8
  • 30
0
votes
0 answers

How do you install a universal variant of an outdated version of a port file?

OS X 10.7.5 I need the universal variant of libjpeg v8 (jpeg @8d_0+universal), but MacPorts seems to insist that if I am to have the universal variant, it must be the latest version (jpeg @9_1+universal) I've looked through the MacPorts…
Brionius
  • 13,858
  • 3
  • 38
  • 49
0
votes
1 answer

Why C# Object passed to COM method's VARIANT* parameter converted to VT_DISPATCH type

I have an ATL COM service which exposes a method that takes in an [in, out] VARIANT pointer as a parameter. For example, I have the following method for a COM interface, say IMyApiCom: HRESULT Action([in, out] VARIANT* EmptyObj); I wrote a C#…
ykay
  • 1,877
  • 19
  • 24
0
votes
1 answer

PHP adodb COM recordset returning variant:__set_state for dates/times, how can I just get them as strings?

I'm accessing an mssql db in PHP and creating the connection as a new COM() object. My query runs perfectly fine. When I do the query in Microsoft SQL Server 2008, everything looks great, the dates/times show up in columns and are readable. But when…
Josh L
  • 39
  • 6
0
votes
1 answer

Marshaling Delphi 5 OleVariant to C#

I'm trying to use some legacy Delphi 5 DLLs from C# (2.0/3.5). Some of the exported functions are declared as such: function SimpleExport: OleVariant; stdcall; function BiDirectionalExport(X: OleVariant; var Y: OleVariant): OleVariant; stdcall; I…
ulrikj
  • 176
  • 2
  • 12
0
votes
1 answer

VB6 - Convert Variant to Node

I am using a For Each loop to go through a Variant array in VB6. At one point, I want to convert the element of the loop (elem), which is a Variant, to a Node. Dim elem as Variant For Each elem In ndArray Dim nodle As Node nodle =…
user2437443
  • 2,067
  • 4
  • 23
  • 38
0
votes
0 answers

Default initialize arbitrary bounded type of boost variant and optional

Default constructor of boost variant initializes the first bounded type. How do I default initialize any member other than the first? One option is to simply assign a default constructed object of the right type. However, that does not work with…
Sumant
  • 4,286
  • 1
  • 23
  • 31
0
votes
2 answers

How do I (or if I can't) use Variants on simple DLLs?

I want to expose some functionality of a internal object as a DLL - but that functionality uses variants. But I need to know: I can export a function with Variant parameters and/or return - or is better to go to an string-only representation? What…
Fabricio Araujo
  • 3,810
  • 3
  • 28
  • 43
0
votes
1 answer

How to make a type safe wrapper around Variant values

I'm working with a OPC Server control that stores data tags as variant types, described by System.Runtime.InteropServices.VarEnum. These types include the following, VT_BSTR (string), VT_I2 (short) and VT_I4 (long). All these values are stored by…
Matt Warren
  • 10,279
  • 7
  • 48
  • 63
0
votes
0 answers

Traversing boost::variant types with Visitor that takes template

I've got a persistence class like: class Writer: private boost::noncopyable { template struct Record { std::vector _queued; // waiting to be persisted hsize_t _fileRows; // on disk DataSet _ds; …
jorgetown
  • 455
  • 3
  • 12
0
votes
1 answer

overflow while converting variant of type (OleStr) into Type Integer Delphi

Im trying to read a Xml file but i keep getting a "overflow while converting variant of type (OleStr) into Type Integer" error on Integer or WideString types, ive tried changing the types from VarToStr, IntToStr,VarToWideStr and OleStrToString. A…
Gis
  • 31
  • 2
  • 5
0
votes
2 answers

Is it possible to have a boost::variant inside a struct that does not get "new"ed?

I have created the struct struct Event { int key; boost::variant value; }; Is it possible to create Events like so: Event e; I have tried this but am getting compiler errors. Is this possible or do i HAVE to do: Event e = new…
Stoff81
  • 597
  • 4
  • 15
0
votes
1 answer

How and when are variant type are converted to regular data types

When the actual data type of a variable will be decided? For ex: x=10 here x will hold integer x="Hello" here x will hold string My basic question is msgbox "2"+"3" is 23 because these are strings and + is for concatenation so the result is 23 Then…
Uday
  • 1,433
  • 10
  • 36
  • 57
0
votes
0 answers

Safe conversion of _variant_t

I need to perform a safe conversion between a _variant_t and a type such as long\bool_bstr_t etc. There are many options to do this, but I'm getting lost in the (poor) documentation. For example for conversion to long: Option 1: Check if the…
user181218
  • 1,655
  • 5
  • 28
  • 42
0
votes
0 answers

VARIANT members not resolved in C++ OLE automation application (Eclipse IDE)

I'm developing a C++ application that reads and writes to Excel using OLE automation (code based on this: http://support.microsoft.com/kb/216686). The application has been up and running just fine when I was using VS2010. However recently I decided…
FadiBitar
  • 11
  • 2