Variant データ型を使用すると、(Float か Integer か、のように)特定の型を扱わずに済みます。 特に、オブジェクト、数字、または文字列のいずれかを格納するパラメータの場合に便利です。
たとえば、SetUserPref コマンドを使用すると、スクリプトから Softimage ユーザ設定の一部を設定できます。コンストレイント コンペンセイション フラグを切り替えるためにこのコマンドを使用する場合は、Value 引数に 0 または 1(整数を)引き渡します。しかし、スクリプトのファイル名を設定する場合は文字列を引き渡します。
| 言語 |
コメント |
|---|---|
| VBScript |
VBScript では、Variant は使用可能なメインのデータ型で、TypeName 関数(Variant ではない)を使用する場合に戻されるいくつかのサブタイプ(Boolean、integer、long など)を持ちます。 VBScript では、自動的に型の強制変換(キャスティング)が実行されます。さらに、特定の事前定義済みの関数(CStr、CLng、CDbl など)を使用した、データ型間の明示的な変換も可能です。 VBScript で(dimension キーワードを使用して)変数を作成すると、その変数は常に variant 型になります。 ' 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 |
Jscript では、値を割り当てずに宣言する変数は、未定義と定義されます。これは、このコンテキストの変数と似ています。 たとえば、このコードの一部は、3 つの JScript 変数をテストします。このうち 1 つは宣言されているが割り当てられていないもので、残りの 2 つには別の値が割り当てられています。 JScript では、値が変数に割り当てられるとすぐに、割り当てられたデータの種類が一致するように、その変数のデータ型が暗示的に強要されます。 それ以外の場合は、未定義になります。
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" |
| PerlScript |
PerlScript には、指定可能な値のタイプのすべてのパターンを網羅する、scalar と呼ばれる不特定型があります。 |
| Python |
Python には、変数値を表す特定の型はありませんが、Softimage のデータ型と Python ネイティブのデータ型との間の暗黙の変換機能は提供されています。 ActivePython で用意されている win32com モジュールにリンクする必要があります(詳細については、http://www.activestate.com/Products/ActivePython/index.plex を参照してください)。 |
| C++ |
Softimage|Softimage C++ API では、C++ API Class Library の Variant 型とよく似た、XSI::CValue と呼ばれるクラスを実装しています。 ただし、これは本物の ActiveX バリアントではなく、Softimage のデータ型と C++ ネイティブのデータ型間でのほとんどのデータ変換を処理するだけのものです。 |
| C# |
C# では、Variants は object 型(System.Object)にマッピングされます。 注:
SAFEARRAYs を伴う Variants の場合、そのオブジェクトを配列に放出することができます。
// 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;
//...
} |
詳細については、「言語間でのデータ タイプを比較する 」の複数の異なる言語間の variant データ型を比較した表を参照してください。