## wxVariant

The wxVariant class represents a container for any type. A variant's value can be changed at run time, possibly to a different type of value.

As standard, wxVariant can store values of type bool, wxChar, double, long, string, string list, time, date, void pointer, list of strings, and list of variants. However, an application can extend wxVariant's capabilities by deriving from the class wxVariantData and using the wxVariantData form of the wxVariant constructor or assignment operator to assign this data to a variant. Actual values for user-defined types will need to be accessed via the wxVariantData object, unlike the case for basic data types where convenience functions such as GetLong can be used.

Pointers to any wxObject derived class can also easily be stored in a wxVariant. wxVariant will then use wxWidgets' built-in RTTI system to set the type name (returned by GetType) and to perform type-safety checks at runtime.

This class is useful for reducing the programming for certain tasks, such as an editor for different data types, or a remote procedure call protocol.

An optional name member is associated with a wxVariant. This might be used, for example, in CORBA or OLE automation classes, where named parameters are required.

Note that as of wxWidgets 2.7.1, wxVariant is reference counted. Additionally, the convenience macros DECLARE_VARIANT_OBJECT and IMPLEMENT_VARIANT_OBJECT were added so that adding (limited) support for conversion to and from wxVariant can be very easily implemented without modifying either wxVariant or the class to be stored by wxVariant. Since assignment operators cannot be declared outside the class, the shift left operators are used like this:

        // in the header file
        DECLARE_VARIANT_OBJECT(MyClass)

        // in the implementation file
        IMPLMENT_VARIANT_OBJECT(MyClass)

        // in the user code
        wxVariant variant;
        MyClass value;
        variant << value;

        // or
        value << variant;

For this to work, MyClass must derive from wxObject, implement the wxWidgets RTTI system and support the assignment operator and equality operator for itself. Ideally, it should also be reference counted to make copying operations cheap and fast. This can be most easily implemented using the reference counting support offered by wxObject itself. By default, wxWidgets already implements the shift operator conversion for a few of its drawing related classes:

    IMPLEMENT_VARIANT_OBJECT(wxColour)
    IMPLEMENT_VARIANT_OBJECT(wxImage)
    IMPLEMENT_VARIANT_OBJECT(wxIcon)
    IMPLEMENT_VARIANT_OBJECT(wxBitmap)

Derived from

wxObject

Include files

<wx/variant.h>

See also

wxVariantData

Members

wxVariant::wxVariant
wxVariant::~wxVariant
wxVariant::Append
wxVariant::Clear
wxVariant::ClearList
wxVariant::Convert
wxVariant::GetCount
wxVariant::Delete
wxVariant::GetArrayString
wxVariant::GetBool
wxVariant::GetChar
wxVariant::GetData
wxVariant::GetDateTime
wxVariant::GetDouble
wxVariant::GetLong
wxVariant::GetName
wxVariant::GetString
wxVariant::GetType
wxVariant::GetVoidPtr
wxVariant::GetWxObjectPtr
wxVariant::Insert
wxVariant::IsNull
wxVariant::IsType
wxVariant::IsValueKindOf
wxVariant::MakeNull
wxVariant::MakeString
wxVariant::Member
wxVariant::NullList
wxVariant::SetData
wxVariant::operator =
wxVariant::operator ==
wxVariant::operator !=
wxVariant::operator []
wxVariant::operator wxChar
wxVariant::operator double
wxVariant::operator wxString
wxVariant::operator void*
wxVariant::operator wxDateTime

### wxVariant::wxVariant

wxVariant()

Default constructor.

wxVariant(const wxVariant& variant)

Copy constructor, uses reference counting.

wxVariant(const wxChar* value, const wxString& name = "")

wxVariant(const wxString& value, const wxString& name = "")

Construction from a string value.

wxVariant(wxChar value, const wxString& name = "")

Construction from a character value.

wxVariant(long value, const wxString& name = "")

Construction from an integer value. You may need to cast to (long) to avoid confusion with other constructors (such as the bool constructor).

wxVariant(bool value, const wxString& name = "")

Construction from a boolean value.

wxVariant(double value, const wxString& name = "")

Construction from a double-precision floating point value.

wxVariant(const wxList& value, const wxString& name = "")

Construction from a list of wxVariant objects. This constructor copies value, the application is still responsible for deleting value and its contents.

wxVariant(void* value, const wxString& name = "")

Construction from a void pointer.

wxVariant(wxObject* value, const wxString& name = "")

Construction from a wxObject pointer.

wxVariant(wxVariantData* data, const wxString& name = "")

Construction from user-defined data. The variant holds onto the data pointer.

wxVariant(wxDateTime& val, const wxString& name = "")

Construction from a wxDateTime.

wxVariant(wxArrayString& val, const wxString& name = "")

Construction from an array of strings. This constructor copies value and its contents.

wxVariant(DATE_STRUCT* val, const wxString& name = "")

Construction from a odbc date value. Represented internally by a wxDateTime value.

wxVariant(TIME_STRUCT* val, const wxString& name = "")

Construction from a odbc time value. Represented internally by a wxDateTime value.

wxVariant(TIMESTAMP_STRUCT* val, const wxString& name = "")

Construction from a odbc timestamp value. Represented internally by a wxDateTime value.

### wxVariant::~wxVariant

~wxVariant()

Destructor.

Note that destructor is protected, so wxVariantData cannot usually be deleted. Instead, DecRef should be called. See reference-counted object destruction for more info.

### wxVariant::Append

void Append(const wxVariant& value)

Appends a value to the list.

### wxVariant::Clear

void Clear()

Makes the variant null by deleting the internal data and set the name to wxEmptyString.

### wxVariant::ClearList

void ClearList()

Deletes the contents of the list.

### wxVariant::Convert

bool Convert(long* value) const

bool Convert(bool* value) const

bool Convert(double* value) const

bool Convert(wxString* value) const

bool Convert(wxChar* value) const

bool Convert(wxDateTime* value) const

Retrieves and converts the value of this variant to the type that value is.

### wxVariant::GetCount

size_t GetCount() const

Returns the number of elements in the list.

### wxVariant::Delete

bool Delete(size_t item)

Deletes the zero-based item from the list.

### wxVariant::GetArrayString

wxArrayString GetArrayString() const

Returns the string array value.

### wxVariant::GetBool

bool GetBool() const

Returns the boolean value.

### wxVariant::GetChar

wxChar GetChar() const

Returns the character value.

### wxVariant::GetData

wxVariantData* GetData() const

Returns a pointer to the internal variant data. To take ownership of this data, you must call its IncRef method. When you stop using it, DecRef must be likewise called.

### wxVariant::GetDateTime

wxDateTime GetDateTime() const

Returns the date value.

### wxVariant::GetDouble

double GetDouble() const

Returns the floating point value.

### wxVariant::GetLong

long GetLong() const

Returns the integer value.

### wxVariant::GetName

const wxString& GetName() const

Returns a constant reference to the variant name.

### wxVariant::GetString

wxString GetString() const

Gets the string value.

### wxVariant::GetType

wxString GetType() const

Returns the value type as a string. The built-in types are: bool, char, date, double, list, long, string, stringlist, time, void*.

If the variant is null, the value type returned is the string "null" (not the empty string).

### wxVariant::GetVoidPtr

void* GetVoidPtr() const

Gets the void pointer value.

### wxVariant::GetWxObjectPtr

wxObject* GetWxObjectPtr() const

Gets the wxObject pointer value.

### wxVariant::Insert

void Insert(const wxVariant& value)

Inserts a value at the front of the list.

### wxVariant::IsNull

bool IsNull() const

Returns true if there is no data associated with this variant, false if there is data.

### wxVariant::IsType

bool IsType(const wxString& type) const

Returns true if type matches the type of the variant, false otherwise.

### wxVariant::IsValueKindOf

bool IsValueKindOf(const wxClassInfo* type type) const

Returns true if the data is derived from the class described by type, false otherwise.

### wxVariant::MakeNull

void MakeNull()

Makes the variant null by deleting the internal data.

### wxVariant::MakeString

wxString MakeString() const

Makes a string representation of the variant value (for any type).

### wxVariant::Member

bool Member(const wxVariant& value) const

Returns true if value matches an element in the list.

### wxVariant::NullList

void NullList()

Makes an empty list. This differs from a null variant which has no data; a null list is of type list, but the number of elements in the list is zero.

### wxVariant::SetData

void SetData(wxVariantData* data)

Sets the internal variant data, deleting the existing data if there is any.

### wxVariant::operator =

void operator =(const wxVariant& value)

void operator =(wxVariantData* value)

void operator =(const wxString& value)

void operator =(const wxChar* value)

void operator =(wxChar value)

void operator =(const long value)

void operator =(const bool value)

void operator =(const double value)

void operator =(void* value)

void operator =(wxObject* value)

void operator =(const wxList& value)

void operator =(const wxDateTime& value)

void operator =(const wxArrayString& value)

void operator =(const DATE_STRUCT* value)

void operator =(const TIME_STRUCT* value)

void operator =(const TIMESTAMP_STRUCT* value)

Assignment operators, using reference counting when possible.

### wxVariant::operator ==

bool operator ==(const wxVariant& value) const

bool operator ==(const wxString& value) const

bool operator ==(const wxChar* value) const

bool operator ==(wxChar value) const

bool operator ==(const long value) const

bool operator ==(const bool value) const

bool operator ==(const double value) const

bool operator ==(void* value) const

bool operator ==(wxObject* value) const

bool operator ==(const wxList& value) const

bool operator ==(const wxArrayString& value) const

bool operator ==(const wxDateTime& value) const

Equality test operators.

### wxVariant::operator !=

bool operator !=(const wxVariant& value) const

bool operator !=(const wxString& value) const

bool operator !=(const wxChar* value) const

bool operator !=(wxChar value) const

bool operator !=(const long value) const

bool operator !=(const bool value) const

bool operator !=(const double value) const

bool operator !=(void* value) const

bool operator !=(wxObject* value) const

bool operator !=(const wxList& value) const

bool operator !=(const wxArrayString& value) const

bool operator !=(const wxDateTime& value) const

Inequality test operators.

### wxVariant::operator []

wxVariant operator [](size_t idx) const

Returns the value at idx (zero-based).

wxVariant& operator [](size_t idx)

Returns a reference to the value at idx (zero-based). This can be used to change the value at this index.

### wxVariant::operator wxChar

char operator wxChar() const

Operator for implicit conversion to a wxChar, using wxVariant::GetChar.

### wxVariant::operator double

double operator double() const

Operator for implicit conversion to a double, using wxVariant::GetDouble.

long operator long() const

Operator for implicit conversion to a long, using wxVariant::GetLong.

### wxVariant::operator wxString

wxString operator wxString() const

Operator for implicit conversion to a string, using wxVariant::MakeString.

### wxVariant::operator void*

void* operator void*() const

Operator for implicit conversion to a pointer to a void, using wxVariant::GetVoidPtr.

### wxVariant::operator wxDateTime

void* operator wxDateTime() const

Operator for implicit conversion to a pointer to a wxDateTime, using wxVariant::GetDateTime.