Declaring and initializing TVarRec (array of const) params - delphi

I want to define a var or const that I can further use in TClientDataSet SetRange method:
var
lRangeStart : array of TVarRec;
lRangeEnd : array of TVarRec;
procedure SetRange;
begin
SetLength (lRangeStart, 2);
SetLength (lRangeEnd , 2);
lRangeStart [0] := Field1; // Incompatible types tVarRect and Integer
lRangeStart [1] := Field2; // Incompatible types tVarRect and Integer
lRangeEnd [0] := Field1; // Incompatible types tVarRect and Integer
lRangeEnd [1] := Field2; // Incompatible types tVarRect and Integer
MyDataSet.SetRange (lRangeStart, lRangeEnd);
end;

That's not the way it works. You create the arrays differently:
MyDataSet.SetRange([Field1, Field2], [Field1, Field2]);
The compiler takes care of generating the various TVarRec records for you.
You can also have mixed types, if the columns in your index are of different types:
MyDataSet.SetRange([1, 'Testing'], [1, 'Testing']);
To use input from the user instead, just assign the the user input to variables, doing any necessary type conversions, and pass in the variables.
var
Val1, Val2: Integer;
begin
Val1 := StrToInt(Edit1.Text);
Val2 := StrToInt(Edit2.Text);
MyDataSet.SetRange([Val1, Val2], [Val1, Val2]);
end;
Or, you can do the conversion in-line, but exceptions due to invalid types are an issue:
MyDataSet.SetRange([StrToInt(Edit1.Text), StrToInt(Edit2.Text)],
StrToInt(Edit1.Text), StrToInt(Edit2.Text)]);
In order to do so yourself, you'd have to explicitly assign to the appropriate type of each element (which defeats much of the purpose of using an array of const, which is being able to create it on-the-fly in your code and mix types):
SetLength(lRangeStart, 2);
lRangeStart[0].vInteger := 1;
lRangeStart[1].vInteger := 2;

Related

Pass static byte array in parameter in Delphi

I have a static array:
myArray: array [0..15] of byte;
I wish to pass this array (not a copy of the array) into a function. Inside the function, I will manipulate the data in array.
Different lengths of STATIC byte array may be passed in to Init at different times. Therefore, I do not wish to declare Init to be able to receive only a certain length of byte array. (e.g. in this case it is a 16 bytes. At other times it may be 65000 bytes)
This is the way I used the function:
Init(#myArray[0]); //passing in pointer to static array
I tried to declare Init like this:
procedure Init(x: array of byte);
begin
//Do some stuff with x, for e.g.
Length(x); //I do not intend to pass in the length as a parameter,
//but determine it in this function itself.
end;
Is this also a correct way of declaring the Init function parameter?
Instead of Init(#myArray[0]) use Init(myArray). Delphi will transfer the myArray by reference
You declared x as open array of bytes. The open array parameters are type compatible with the array variables with the same element type. Delphi will transfer fixed array length as a hidden parameters. You can access it with Length(x) and e.g. for i := Low(x) to High(x) do.
Open array parameters cannot be set to nil. If you need to set x to nil, then you have to declare x as pointer to Byte or pointer to array[..] of Byte.
For static arrays there is no information about their length at runtime, but only at compile time.
So if you loose the type information at passing a static array to a function, there is no way to find out the length of the array within the function.
=> If you want to use static arrays with variable sizes, you must pass the size as additional parameter.
The clean way would be to work with pointer aritmetic rather than arrays here.
PByte = ^byte;
procedure Init(arr: PByte; len: Integer);
var
i: Integer;
begin
//Write 0, 1, 2 ... to arr
for i:= 0 to len-1 do
begin
arr^:= i;
Inc(arr);
end;
end;
Unclean solution:
TByteArray = array [0..$FFFFFFFF] of Byte;
PByteArray = ^TByteArray;
procedure Init(arr: PByteArray; len: Integer);
begin
arr^[0]:= 0;
arr^[1]:= 1;
...
end;
...
var
myAray: array[0..10] of byte;
begin
Init(PByteArray(#myArray[0]), 10);
end;
First, declaring
procedure Init(x: array of byte);
declare an open array parameter (not a dynamic array, unlike some other stated).
To do what you want, you need to declare like this :
type
TMyArray: array [0..15] of byte;
var
MyArray : TMyArray;
Procedure Init(var X : TMyArray)
Still, I'm not sure why you try to set the variable to nil. A static array variable is a little bit like a record, you can't set it to nil. And IF you pass a pointer to the function instead, you will need to assign your array to a variable first anyway(or allocate the memory directly in the function). Like this :
type
TMyArray : Array[0..15] of byte;
pTMyArray : ^TMyArray;
Procedure Init(var X : pTMyArray);
procedure DoSomething;
var P : pTMyArray;
begin
New(P); //or if you array is global, simply do "P := #MyArray"
try
//code here
Init(P);
//code here
finally
Dispose(P);
end;
end
But that would be a bad construct IMO, because if you set your array pointer to nil inside the Init function, you will get a memory leak. So this is pretty bad to start with. But I guess if it's your Init function that reserve the memory that would be OK.
If you settle for an open array parameter, I'd suggest using the Low and High to determine the bounds of the array.
I think that's the best info I can give you until you give more detail on what you are trying to achieve.
EDIT
Ok, if you need the function to accept an array of different size, you need an open array parameter.
You did it right in your exemple. But you can't set it to nil within the function.
I don't think it's possible to pass to a function a pointer to an open array type. If you really want a pointer to set it to nil, you will need to pass it as PByteArray... But you will need to also pass the size of the array.
first when your are declarating this
procedure Init(x: array of byte);
you are using a dynamic array open array parameter instead of a static array.
to pass a static array as a parameter , you must define a type
like this
type
Arr15 = array [0..15] of byte;
and then declare the procedure like this, using the keyword var to pass by reference.
procedure Init(var x: Arr15);
about the nil assign , you cannot set to nil a static array.
another alternative is use a dynamic array
check this sample
type
ByteArray = array of byte;
procedure Init(var x : ByteArray);
const
LenArray=15;
var
i : Byte;
begin
SetLength(x,LenArray); //alocate the memory and set the length of the array.
for i:=0 to LenArray-1 do //fill the array
x[i]:=i;
Writeln(Length(x)); //show the len of the array
SetLength(x,0); //now dealocates de memory
Writeln(Length(x)); //show the len of the array again
end;
Var
myArray : ByteArray;
begin
try
Init(myArray);
Readln;
except
on E:Exception do
Writeln(E.Classname, ': ', E.Message);
end;
end.

How to convert between TVarRec and Variant?

Is there a standard way to convert between TVarRec and Variant values?
I want to parse an 'array of const' and use the values to populate parameters in a TMSQuery. To do this I'm using a list of column names (generated from TMSQuery.KeyFields), and matching the values in the array with the column names in KeyFields (by position), then using the column name to set the corresponding parameter using ParamByName.
The code below is what I've come up with, but VarRecToVariant doesn't seem very elegant. Is there a better solution?
keyFields: TStringList;
// List of table column names (keyFields.DelimitedText := query.KeyFields;)
// e.g. Name, Age
query: TMSQuery;
// Parametrized query with a parameter for each field in keyFields
// SELECT * FROM People WHERE Age=:Age AND Name=:Name
// If keyValues is ['Bob', 42] the resulting query should be
// SELECT * FROM People WHERE Age=42 AND Name='Bob'
procedure Read(keyValues: array of const);
var
i: Integer;
name: string;
value: Variant;
begin
...
for i := 0 to keyFields.Count - 1 do
begin
name := keyFields[i];
value := VarRecToVariant(keyValues[i]);
query.ParamByName(name).Value := value;
end;
query.Open
...
end;
function VarRecToVariant(varRec: TVarRec): Variant;
begin
case varRec.VType of
vtInteger: result := varRec.VInteger;
vtBoolean: result := varRec.VBoolean;
vtChar: result := varRec.VChar;
vtExtended: result := varRec.VExtended^;
vtString: result := varRec.VString^;
...
end;
end;
Notes:
The values in the array of const depend on the parameters in the query. The caller knows what these are, but the method that uses the array doesn't know how many or what type to expect. I.e. I can't change the method to Read(name: string; age: integer).
The parameters are not necessarily used in the same order that the values are specified in the array of const. In the example, keyFields are specified as "Name,Age" but the query uses Age before Name. This means Params[i].Value := keyValues[i] won't work. I think VarRecToVariant would still be needed anyway, which I'm trying to avoid).
Replace
procedure Read(keyValues: array of const);
with
procedure Read(keyValues: array of Variant);
Then you will not need to convert TVarRec to Variant.

Dynamic Arrays and pointers in Delphi

How can i rewrite this C++ code in Delphi?
int *intim = new int[imsize];
unsigned char *grays = new unsigned char[imsize];
int *intim2 = intim;
How can I increment pointer like this:
*(intim++) = x;
In Delphi you can use pointers (like in C/C++) but usually you try to avoid it.
the code looks most like
uses
Types;
procedure Test();
var
intim: TIntegerDynArray;
grays: TByteDynArray;
P: PInteger;
i, s: Integer;
begin
// 'allocate' the array
SetLength(intim, imsize);
SetLength(grays, imsize);
// if you want to walk through the array (Delphi style)
for i := Low(intim) to High(intim) do intim[i] := GetValueFromFunction();
// or (read only access, sum the array)
s := 0;
for i in intim do Inc( s, i );
// or with pointers:
P := #intim[ 0 ];
for i := 0 to High(intim) do begin
P^ := x;
Inc( P ); // like P++;
end;
end;
The people who point out that you should use array types rather than a direct pointer manipulation done as you see it done above in C, are right, it is not idiomatic to Delphi to use dangerous pointer types when safe arrays are easier, can be verified faster, and are safer at runtime. However for the pedantic out there who want to avoid using the nice built in array types, it is possible, albeit stupid, to do so:
var
intim,intim2:PInteger;
x:Integer;
begin
x := 0;
intim := AllocMem(SizeOf(Integer)*imsize);
intim2 := intim;
// dereference pointer intim2, store something, then increment pointer
intim2^ := x;
Inc(intim2);
FreeMem(intim);
The best way would be to use arrays. If imsize is a constant, you want a static array, otherwise, you'll use a dynamic array. Here's the syntax for both:
Static:
var
intim: array[0..imsize - 1] of integer;
Dynamic:
var
intim: array of integer;
begin
setLength(intim, imsize);
//do something with intim
end;
As for grays, how you'll declare it depends on if you're using your array of "unsigned chars" as characters (a string) or as single-byte integers. If they're integers, you can declare an unsigned single-byte integer as byte, and declare an array (either static or dynamic) of them using the syntax described above. If they're characters, just use the string type.
And the pointer math is possible, but not recommended because it makes buffer overruns too easy. Instead, try declaring your other variable as an integer and use it as an index into the array. If you have bounds checking turned on, this will prevent you from going beyond the end of the array and corrupting memory.

Is there an easy way to clone a string array?

I have an array declared as:
const A: array[0..3] of ShortString = (
'Customer',
'Supplier',
'Stock',
'GL'
);
var B: array of ShortString;
I would like to clone the string array A to another array B. Using Move or Copy function doesn't work. Is there a fast and easy way to clone the array without using for loop?
The problem you face is that your constant A and your variable B are actually of different types. This can be demonstrated most easily by showing how you would declare a const and a var of the same type in a fashion equivalent to what you show in your question:
type
TSA = array[0..3] of ShortString;
const
A: TSA = (
'Customer',
'Supplier',
'Stock',
'GL');
var B: TSA;
With these declarations you could then simply write:
B := A;
But when A is a dimensioned array and B is a dynamic array, this isn't possible and your only option is to SetLength(B) as required and copy the elements one-by-one.
Although the const and the var types may look like they are the same - or compatible types - they are not, and this then is no different from trying to assign an Integer constant to a String variable... even though you know the simple conversion required to achieve it, the compiler cannot presume to guess that you intended this, so you have to be explicit and provide the conversion code yourself.
Something like:
SetLength(B, Length(A));
for i := Low(A) to High(A) do
B[i] := A[i];
Or in a more generic way:
type
TStringArray = array of ShortString;
procedure CloneArray(const source: array of ShortString; var dest: TStringArray);
var
i: integer;
begin
SetLength(dest, Length(source));
for i := Low(source) to High(source) do
dest[i] := source[i];
end;
In the latter case you'll have to redeclare B as B: TStringArray.

String representation of the content type of a Variant?

First, apologies for my English, I hope it makes sense what I`ve written here. Now to my problem.
How can I get the string representation of the content type of a Variant using TypInfo.GetEnumName(). I have tried the following, without luck, I get a numeric representation.
myString := GetEnumName( TypeInfo(TVarType), TVarData(myVar).VType );
Thank you.
Just use the build-in Delphi function for getting the string representation of a Variant type.
var
MyVariantType: string;
MyVariant: Variant;
begin
MyVariant := 'Hello World';
MyVariantType := VarTypeAsText(VarType(MyVariant));
ShowMessage(MyVariantType); //displays: String
MyVariant := 2;
MyVariantType := VarTypeAsText(VarType(MyVariant));
ShowMessage(MyVariantType); //displays: Byte
end;
Quoting from the Delphi 2007 help:
Use GetEnumName to convert a Delphi enumerated value into the symbolic name that represents it in code.
That means that you can't use it for that purpose, as TVarData.VType is not an enumerated value, but an integer which is set to one of the constants in System.pas that are taken from the Windows SDK wtypes.h file. Look at the source of GetEnumName(), it does immediately return a string containing the value of the integer.
Edit:
is there any other way to get the string representation of TVarData.VType
You can determine the string representation manually. First you need to be aware of that there are several bits of information encoded in that integer, so a simple case statement or array lookup will not work. The lower 12 bits are the type mask, and the upper bits encode information about whether it is a vector or array type and whether it is given by reference or not. The important parts are:
const
varTypeMask = $0FFF;
varArray = $2000;
varByRef = $4000;
So you could do something like:
function VariantTypeName(const AValue: TVarData): string;
begin
case AValue.VType and varTypeMask of
vtInteger: Result := 'integer';
// ...
end;
if AValue.VType and varArray <> 0 then
Result := 'array of ' + Result;
if AValue.VType and varByRef <> 0 then
Result := Result + ' by ref';
end;
Since it's not an enum, you'll have to do it manually. Write something like this:
function VariantTypeName(const value: TVarData): string;
begin
case value.VType of
vtInteger: result := 'integer';
//and so on
end;
Or, since the values in System.pas are listed in order, you could try declaring a const array of strings and have your VariantTypeName function return the appropriate member of the array.
Here's a thought for Delphi versions that don't support VarTypeAsText: You could define a enumerate type yourself that follows the VType values:
type
{$TYPEINFO ON}
TMyVarType = (
varEmpty = System.varEmpty,
varNull = System.varNull,
// etc...
);
(Fill the unused enum slots too - see Why do I get "type has no typeinfo" error with an enum type for the reasoning behind this).
Next, use these functions to read the Variants' type as your own enumerate type :
function MyVarType(VType: TVarType): TMyVarType; overload;
begin
Result := TMyVarType(VType);
end;
function MyVarType(V: Variant): TMyVarType; overload;
begin
Result := TMyVarType(TVarData(V).VType);
end;
And then you can convert it to a string like this :
function VarTypeToString(aValue: TMyVarType): string;
begin
Result := GetEnumName(TypeInfo(TMyVarType), Ord(aValue));
end;

Resources