SRL Archive - OSR Intermediate Scripting Tutorials

Variants and TVariantArrays!


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.

ZephyrsFury

2008-04-16 08:42:53

Nice tut. :D Nicely set out and concise. I never knew about this VarType function. I learned something new today. :)

Thanks.

StrikerX

2008-04-16 08:43:08

Nice long tut i love them

is this what you use in Forms?

Rep++

Santa_Clause

2008-04-16 08:47:32

Wow. Fast replies. Thanks guys.

From: ZephyrsFury
Nice tut. :D Nicely set out and concise. I never knew about this VarType function. I learned something new today. :)

Thanks.


No problem. VarType isn't really used much, but it's probably the most useful function when you're using Variants.

From: strikerx25
Nice long tut i love them

is this what you use in Forms?

Rep++


Thanks for the reputation. TVariantArrays are used in forms, but it isn't really code that's meant to be understood, because it's always used in exactly the same way, and any change in the default form code will cause the form not to work.

EvilChicken!

2008-04-16 10:59:40

Wow, nice tut. I only remember to have read one tut about this before - but this one is better. Really nice! Post a couple useful functions that use TVariantArrays, please?

Wizzup?

2008-04-16 11:54:51

ThreadSafeCall
SetTimeOut

n3ss3s

2008-04-16 12:35:22

Something that you also can use if you really don't want to make the params of some function of yours go out of the margin on the 'defining line' of the function -


Program VariantTest;

Var
V: Variant;

begin
V := 1;
Writeln(BoolToStr(V));
end.

;)

Dan Cardin

2008-04-16 18:33:40

wow, i knew of varients, but never tried to understand/use them. I betcha i could find a useful application for them :(

mastaraymond

2008-04-17 10:49:48

From: Dan Cardin
wow, i knew of varients, but never tried to understand/use them. I betcha i could find a useful application for them :(

Login.scar :p I used 'em there.


You can use em like "dynamic records"

EvilChicken!

2008-04-17 11:35:26

From: mastaraymond
Login.scar :p I used 'em there.


You can use em like "dynamic records"


HA! Login.scar! Thats where I remembered them from. (In a script for me to learn from.) Everyone should look at login.scar for examples on TVariantArrays.

Lee Lok Hin

2008-04-18 07:31:32

Thanks, never knew this type of thing existed.

Rep+

Press Play

2008-04-18 12:11:35

nice, thanx for that…ill see if one my scripts can use it :)

Miitchyy

2008-04-25 09:16:30

Don't forget you can't user defined Types into a variant, like AUser(If it's still called that) or TObject

Metagen

2008-08-13 13:15:07

Excellent tutorial. very to the point. It was also perfectly clear. For this i shall rep you and you shall be the first that i have EVER repped. Congrats :p

sirlaughsalot

2008-08-13 13:46:03

Very nice tut, but what would you use this stuff for? It seems really cool but i cant really think of anything for this to be useful for…