PDA

View Full Version : Variants and TVariantArrays!



Santa_Clause
04-16-2008, 12:38 PM
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
04-16-2008, 12:42 PM
Nice tut. :D Nicely set out and concise. I never knew about this VarType function. I learned something new today. :)

Thanks.

StrikerX
04-16-2008, 12:43 PM
Nice long tut i love them

is this what you use in Forms?

Rep++

Santa_Clause
04-16-2008, 12:47 PM
Wow. Fast replies. Thanks guys.


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.


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!
04-16-2008, 02:59 PM
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?
04-16-2008, 03:54 PM
ThreadSafeCall
SetTimeOut

n3ss3s
04-16-2008, 04:35 PM
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
04-16-2008, 10:33 PM
wow, i knew of varients, but never tried to understand/use them. I betcha i could find a useful application for them :(

mastaraymond
04-17-2008, 02:49 PM
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!
04-17-2008, 03:35 PM
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
04-18-2008, 11:31 AM
Thanks, never knew this type of thing existed.

Rep+

Press Play
04-18-2008, 04:11 PM
nice, thanx for that...ill see if one my scripts can use it :)

Miitchyy
04-25-2008, 01:16 PM
Don't forget you can't user defined Types into a variant, like AUser(If it's still called that) or TObject

Metagen
08-13-2008, 05:15 PM
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
08-13-2008, 05:46 PM
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...