Santa_Clause
2008-04-16 08:38:20
Variants
A variant can be simply put as an undefined variable. What does this mean?
Refer to your knowledge of arrays. When declaring an array, it is not always essential to specify the length of it when declaring the variable.
A variant is a variable, whose type you don’t have to declare. This means that whenever using variants in SCAR, you can treat the parameter as any type of variable, from a String to an Extended value.
Take this piece of code:
Var
VariantTest : Variant;
Begin
VariantTest := 'SCAR';
VariantTest := 150;
VariantTest := True;
End.As stated before, a Variant is an undefined variable, so you can treat it as any type of variable you like. In the example, the variant is set to a String ‘SCAR’, followed by an Integer ‘150’ and then is set to a Boolean ‘True’.
TVariantArrays
Now that we have understood what a Variant is, we can delve into TVariantArrays. A TVariantArray is an array of Variants, or more simply put as an array of undefined variables.
Take this piece of code:
Var
VariantArrayTest : TVariantArray;
Begin
VariantArrayTest := ['SCAR', 150, True];
End.One useful feature in SCAR is the fact that it can convert Variants into Strings when doing a WriteLn. WriteLn(Variant) would work, and so by declaring a TVariantArray, it is possible to WriteLn all of the values in the array without using any variable conversion functions such as IntToStr.
Basically speaking, even though VariantArrayTest[1] is an Integer ‘150’, you can WriteLn(VariantArrayTest[1]), but you can not WriteLn(150).
VarType
An extremely useful function in SCAR is that of VarType. When using VarType, you insert a Variant and SCAR returns what kind of variable that the Variant is semi-defined as.
VarType is a function that returns an integer, so it can be hard to tell what type of variable VarType is returning when it gives a number. The four most commonly known results of VarType are:
- 256 : String
- 3 : Integer
- 5 : Float
- 11 : Boolean
Take this piece of code:
Var
VariantArrayTest : TVariantArray;
I : Integer;
Begin
VariantArrayTest := ['SCAR', 150, True, 1.5];
For I := 0 To High(VariantArrayTest) Do
Case VarType(VariantArrayTest[I]) Of
256 : WriteLn(VariantArrayTest[I] + ' is a String Value');
3 : WriteLn(VariantArrayTest[I] + ' is an Integer Value');
5 : WriteLn(VariantArrayTest[I] + ' is an Extended Value ');
11 : WriteLn(VariantArrayTest[I] + ' is a Boolean Value ');
End;
End.At first, all the values of TVariantArrayTest are declared. After that, the script loops through every single value in the TVariantArray, checking the VarType for each one, then doing a WriteLn of what kind of variable each value in the array is.
__________________________________
Well, that sums up pretty much everything you need to know about TVariants and TVariantArrays. There aren't many uses for them, but I have no doubt you can find a use for them if you think long enough. For now, I hope you learnt something. Thankyou, and be on standby for the next tutorial.