Delphi encapsulates the entire world of variants into just a handful of functions and procedures, now we need to learn how to use all of these fun functions
Using Variants and Variant Arrays
The first thing we are going to do is learn how to make a Variant Array. Let us look at the following code (below).
This code shows an example of how to create a variant array that contains the VarType varByte. I then populate the array by looped from the lowest boundary of the 1st dimension to the highest boundary of the 1st dimension. I then set each part I index to a random value from 0 to 99. When I am finished with the variant array, I then call VarClear to clear it out. This function will make a few low level calls and free up the memory these variants are occupying.
Make sure you always put a try / finally block around your variant arrays when you create and populate them.
If your not going to finish with them right away, you will want to use a try / except block just in case something crashing during population. This way, you can at least free up the variant without causing any harm.
try
V := VarArrayCreate([0, 5], varByte);
for Index := VarArrayLowBound(V, 1) to
VarArrayHighBound(V, 1) do
V[Index] := Random(100);
finally
VarClear(V);
end;
|
For a better understanding of how variants, variant arrays, and the functions/procedures used to manipulate them can be used, refer to:
Download: Variant Code Example 1.
The only purpose of this class was to cover variant arrays. The concept was not very difficult, but there is a lot of different things you can do with them and nothing beats playing with a new concept before you move on to more difficult ones. That is why this week we are going to play with variant arrays for your homework assignment. Please note that I have created my own term for a certain type of variant array structure. I could not find a term for it so I coined one myself. A compounded variant array is a variant array that acts as a container for other variant arrays.
You are now ready for this lesson's homework assignement...
Next page > Homework Assignment > Page 1, 2, 3
An Introduction to COM Programming with Delphi: Table of Content
<< Previous Lesson (4): A Com Object walk-a-bout. A Class Factory tour. Our first true COM Object program.
>> Last Lesson (6): Let's see how to make your first Type Library using Delphi.