R0b0t1
11-10-2007, 02:54 PM
A TVariantArray is like a Python list (I'd give you a link, but my nets down right now *Google*). In Python, a list is an array of non-related data types. Why should you care? Because it might be the next best thing since TPointArrays (The sliced bread of Object Finding).
So... You probably don't want to know all the technical stuff so I guess I can just tell you how to use them. Its really simple, actually.
program TVA;
var
TestTVA: TVariantArray
begin
end.
Thats how simple it is to create one. Assigning one is just as simple:
program TVA;
var
TestTVA: TVariantArray
begin
TestTVA:= [1337];
end.
So, say we wanted to access that value. It'd be considered an integer, but you don't really have to treat it as one.
program TVA;
var
TestTVA: TVariantArray
begin
TestTVA:= [1337];
WriteLn(TestTVA[0]);
end.
And that would print "1337" in the debug box. Notice how that the value in TestTVA's first index is an integer though. This is a blessing and a curse: While it is easier to I don't know... ...not put quotes around something, it can be a headache for those with strong backgrounds in type-oriented programming. Although, you won't get errors if you cast it to an integer - rather, its just not needed. Now we can look at multi-type TVA's, the ones that truly use its full potential.
So, say we want to add another element:
program New;
var
TestTVA: TVariantArray;
I: Integer;
begin
TestTVA:= [1337, 'leet'];
end.
This makes this variant array what many though previously impossible: two distinct types in the same object. This leads us to one of a TVA's uses -- storing information whose type you may not know. To use this feature we can use a simple for loop.
program New;
var
TestTVA: TVariantArray;
I: Integer;
begin
TestTVA:= [1337, 'leet'];
For I:= 0 to 1 do
WriteLn(TestTVA[I]);
end.
Notice anything different? I didn't have to use any testing to figure out if I needed to use IntToStr or other casting method. Amazing, huh? This could be useful for ... oh, maybe just recieving raw values. Invent your own Ideas for once XD.
Next up, lets see how it fares with records...
program New;
var
TestTVA: TVariantArray;
TestTP : TPoint;
begin
TestTP.x:= 1;
TestTP.y:= 1;
TestTVA:= [1337, 'leet', TestTP];
end.
Ouch! If you try to compile that, you may notice you get a Runtime type mismatch. Seems like you can't insert records into TVAs. We might have better luck with arrays, though. (As you can see, this is like bracket-defining a multi-dimensional array. Except theres no types...)
program New;
var
TestTVA: TVariantArray;
begin
TestTVA:= [1337, 'leet', [1, 2, 3]];
end.
Success! It compiles. I'm guessing you can access it the same way...
program New;
var
TestTVA: TVariantArray;
begin
TestTVA:= [1337, 'leet', [1, 2, 3]];
WriteLn(TestTVA[2][1]);
end.
Dang. Gives and exception. Even with a cast! Dang.. Well, maybe with string arrays... Nope (Tried it, but it compiles, just can't write it). Sad. Thought it might work :\. Well, I guess you could try adding pre-defined arrays to it, not much different though.
( some code later )
Yes, it doesn't print it out, but it will compile it.
Well, can't think of anything I haven't told you. Hope this has taught you something! If I think of anything or someone asks about something, I'll be sure to add it.
So... You probably don't want to know all the technical stuff so I guess I can just tell you how to use them. Its really simple, actually.
program TVA;
var
TestTVA: TVariantArray
begin
end.
Thats how simple it is to create one. Assigning one is just as simple:
program TVA;
var
TestTVA: TVariantArray
begin
TestTVA:= [1337];
end.
So, say we wanted to access that value. It'd be considered an integer, but you don't really have to treat it as one.
program TVA;
var
TestTVA: TVariantArray
begin
TestTVA:= [1337];
WriteLn(TestTVA[0]);
end.
And that would print "1337" in the debug box. Notice how that the value in TestTVA's first index is an integer though. This is a blessing and a curse: While it is easier to I don't know... ...not put quotes around something, it can be a headache for those with strong backgrounds in type-oriented programming. Although, you won't get errors if you cast it to an integer - rather, its just not needed. Now we can look at multi-type TVA's, the ones that truly use its full potential.
So, say we want to add another element:
program New;
var
TestTVA: TVariantArray;
I: Integer;
begin
TestTVA:= [1337, 'leet'];
end.
This makes this variant array what many though previously impossible: two distinct types in the same object. This leads us to one of a TVA's uses -- storing information whose type you may not know. To use this feature we can use a simple for loop.
program New;
var
TestTVA: TVariantArray;
I: Integer;
begin
TestTVA:= [1337, 'leet'];
For I:= 0 to 1 do
WriteLn(TestTVA[I]);
end.
Notice anything different? I didn't have to use any testing to figure out if I needed to use IntToStr or other casting method. Amazing, huh? This could be useful for ... oh, maybe just recieving raw values. Invent your own Ideas for once XD.
Next up, lets see how it fares with records...
program New;
var
TestTVA: TVariantArray;
TestTP : TPoint;
begin
TestTP.x:= 1;
TestTP.y:= 1;
TestTVA:= [1337, 'leet', TestTP];
end.
Ouch! If you try to compile that, you may notice you get a Runtime type mismatch. Seems like you can't insert records into TVAs. We might have better luck with arrays, though. (As you can see, this is like bracket-defining a multi-dimensional array. Except theres no types...)
program New;
var
TestTVA: TVariantArray;
begin
TestTVA:= [1337, 'leet', [1, 2, 3]];
end.
Success! It compiles. I'm guessing you can access it the same way...
program New;
var
TestTVA: TVariantArray;
begin
TestTVA:= [1337, 'leet', [1, 2, 3]];
WriteLn(TestTVA[2][1]);
end.
Dang. Gives and exception. Even with a cast! Dang.. Well, maybe with string arrays... Nope (Tried it, but it compiles, just can't write it). Sad. Thought it might work :\. Well, I guess you could try adding pre-defined arrays to it, not much different though.
( some code later )
Yes, it doesn't print it out, but it will compile it.
Well, can't think of anything I haven't told you. Hope this has taught you something! If I think of anything or someone asks about something, I'll be sure to add it.