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

Variant string conversion in C/C in WinPE environment

I have C/C++ code issue wherein there's WMI query which retrieves instances of Win32_DiskDrive and gets its Size property. This property is retrieved as Variant type and then I have to convert it to UINT64 type which is failing. If I print variant…
Aniket K
  • 1
  • 4
0
votes
2 answers

Using COM object from C++ that in C#.NET returns object []

I have a COM object that I'm trying to use from C++ (not .NET), and all of the example programs and manual are written assuming the use of C#.NET or VB.NET. COM is new to me so I'm a bit overwhelmed. I'm using #import on the TLB but am struggling to…
bdk
  • 4,769
  • 29
  • 33
0
votes
1 answer

How to compare VARIANTARG types

I'm trying to build a function to assign VARIANTARG types to a DISPPARAMS structure, and I'm having some issues figuring out how to check the actual type of the arg. myFunc(int count, const BYTE* types, ...) { DISPPARAMS dParams; …
Matt McMinn
  • 15,983
  • 17
  • 62
  • 77
0
votes
1 answer

Python: parse VARIANT (?)

I have to read a file in python that uses Microsoft VARIANT (I think - I really don't know much about Microsoft code :S). Basically I want to know if there are python packages that can do this for me. To explain - the file I'm trying to read is just…
mathematical.coffee
  • 55,977
  • 11
  • 154
  • 194
0
votes
1 answer

VBA UDF Variant/Integer and Variant/String Arrays are printing only the first value to output cells

The following works great (thanks to gracious assistance by this community!) Function RangeToArrayToRange(inputRange as Range) As Variant Dim inputArray As Variant inputArray = inputRange RangeToArrayToRange =…
nightTrevors
  • 639
  • 4
  • 11
  • 24
0
votes
2 answers

Is there a replacement for variant data type in .NET?

I would like to know if there is a variant data type in Delphi prism. I am converting a Delphi win32 application for Delphi Prism .NET environment.
ThN
  • 3,235
  • 3
  • 57
  • 115
0
votes
2 answers

COM: Access violation making a SafeArray of BSTRs

The following code worked just fine thank you with one COM client, but with a new client (the updated version of the same software) string_array_to_bstr_safearray_variant throws an access violation and everything dies. Can anyone tell me if I'm…
Sideshow Bob
  • 4,566
  • 5
  • 42
  • 79
0
votes
1 answer

Memory allocation for a variant-typed token

I am making an interpreter for a custom programming language (concatenative, soft-typed) and for that purpose I have a central datatype Token. A Token can be of one of the many different types, either of scalar ones or vector ones. To minimize the…
user267885
-1
votes
0 answers

Lazy vs eager loading?

I have an app with a product with many variants, when I load the product page, it is loaded fast, and when I select a variant of that product the image is loaded in about 3s (each product has at least 30 variants) The variant images were loaded in…
Bionix1441
  • 2,135
  • 1
  • 30
  • 65
-1
votes
2 answers

Does the standard or some Boost library offer a compile-time variant?

I'm not asking how to Get type of a std::variant member at a given index during compile time. I'm asking whether the standard or some boost library offer a std::variant-like class whose contained type is known at compile time. What I'm asking, I…
Enlico
  • 23,259
  • 6
  • 48
  • 102
-1
votes
2 answers

Algorithm to sort various data types

Conceptually, I would like to sort data of a single column of variant type, similar to how this is done in Excel: Here we can see that Numbers are sorted before Strings before Booleans before Errors. So, I would like to have a function that works…
David542
  • 104,438
  • 178
  • 489
  • 842
-1
votes
1 answer

Calculate variant of the numbers / TypeError: 'int' object is not callable

I get the TypeError: 'int' object is not callable on the line 27 (var = ...) the file data1_.txt is just 5 lines with a integer number in each line. Can someone help me to understand why? line_number = 0; countline = 0; linesum = 0; avg = 0; var =…
clicktac
  • 13
  • 5
-1
votes
1 answer

Data from viewbag is not displaying properly when using a swatch instead a drop down menu

Using C# ASP.NET MVC for this. I am working on a Product Detail Page. If a product has multiple variations then a drop down appears so the user can navigate to the other products. So far this works using drop down, all records with are populating…
CodeQuest
  • 121
  • 1
  • 14
-1
votes
1 answer

C++ builder: How to convert TRectF to Variant

I am using C++ Builder 10.2.3 (Rad Studio 10.2.3) I am trying to convert a TRectF to a Variant. But it seems not to be working: 1st Solution: using TValue::From(rect).AsVariant() or AsType(). I am getting the error message: Invalid Class…
Louis
  • 75
  • 6
-1
votes
1 answer

SAPUI5 Variant Management Save Button

expert. I use the API 「sap.ui.comp.varinats」 to implement variant management.Save the variant data to S/4 via OData. The "SAVE" button is invisible, so I want to enable it. I referred to the article below, but it didn't…
taguchi
  • 1
  • 1