Variant

 
 
 

Using the Variant data type allows you to postpone dealing with specific type (like Float versus Integer). It is especially useful for parameters that may hold either objects, numbers, or strings.

For example, the SetUserPref command allows you to set some of the Softimage User Preferences through scripting. If you are using this command to toggle the Constraint Compensation Flag, you pass either 0 or 1 (integers) in the Value argument; however, if you are trying to set the name of the scripting filename, you pass a string.

Language

Comments

VBScript

In VBScript, Variant is the main data type available, with several subtypes (Boolean, integer, long, etc.) which are returned when you use the TypeName function, (ie., not Variant).

VBScript automatically performs type coercion, or casting, but also allows you to explicitly convert between data types using special pre-defined functions (CStr, CLng, CDbl, etc.). When you create a variable (with the dimension keyword) in VBScript, it is always the variant type:

	' First you declare the variable (always variant)
	dim myIndex 
	dim myNumber, myLetter
	' When you assign a value, VBScript decides what 
	' subtype it is (based on the value)
	myIndex = 0
	myNumber = 365.77351
	myLetter = "A"
	' See what TypeName() thinks it is...
	testThing myIndex, "myIndex"
	testThing myNumber, "myNumber"
	testThing myLetter, "myLetter"
	function testThing( in_Thing, in_Name )
		LogMessage in_Name & " is a(n) " & TypeName( in_Thing )
	end function
	' The output of the above script is:
	'INFO : "myIndex is a(n) Integer"
	'INFO : "myNumber is a(n) Double"
	'INFO : "myLetter is a(n) String"

JScript

In Jscript, any variable that you declare without assigning any value is considered to be undefined, which is similar to variant in this context. For example, this code snippet tests the three JScript variables, one of which is declared but not assigned, while the other two are assigned different values. As soon as a value is assigned to a variable, JScript implicitly coerces the data type of that variable to match the kind of data that was assigned. Otherwise, it is undefined:

	var myEmptyVar;
	var myBoolean = true;
	var myString = "This is text.";
	testType( myEmptyVar, "myEmptyVar" );
	testType( myBoolean, "myBoolean" );
	testType( myString, "myString" );
	function testType(in_Thing, in_Name)
	{
		if (typeof(in_Thing) == "undefined")
		{
			LogMessage( in_Name + " is undefined." );
		}
		else
		{
			LogMessage( in_Name + " is a " + typeof(in_Thing) );
		}
	}
	// Output of the above script is:
	//INFO : "myEmptyVar is undefined."
	//INFO : "myBoolean is a Boolean"
	//INFO : "myString is a string"

Python

Python does not have a specific type that represents variant values, but provides implicit conversion between Softimage data types and Python native data types. You need to link to the win32com module that comes with ActivePython (for more information, see http://www.activestate.com/Products/ActivePython/index.plex).

C++

The Softimage|Softimage C++ API implements a class called XSI::CValue, which approximates a type of Variant for the C++ API Class Library. However, this is not a real ActiveX variant; it simply handles most data conversion between Softimage data types and C++ native data types.

C#

Variants are mapped to the object type (System.Object) in C#.

Note

In the case of Variants that hold SAFEARRAYs, you can cast the object to an Array:

// The Context.GetAttribute method returns a Variant which, in
// the case of the Arguments attribute, is an array
public bool Execute( Context in_ctxt )
{
	Array args = (Array)in_ctxt.GetAttribute("Arguments");
	// equivalent to :
	//Object rtn = in_ctxt.GetAttribute("Argument");
	//Array args = (Array)rtn;
	//...
}
Tip

See Comparing Data Types across Languages for a table that compares the variant data type across several different languages.

For high-level information about how these languages approach data type in general, see one of the following topics:

Creative Commons License Except where otherwise noted, this work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License