Delphi var parameter setting length comes from the inside of the program

I am trying to pass some strings to a dynamic string array, in this case it is working:

 procedure DoSomeThing(in_cmd: string; var out_strs: array of string);
begin
..
for n := low(out_strs) to high(out_strs) do
begin< br /> out_strs[n] :='bla bla';
end;
end;

Use as in the application:

.
.
.
SetLength(my_out_str, sizer);

DoSomeThing('do it now', my_out_str);
.
.

But I want to use the SetLength of my_out_str in the program.
Is it possible?

Yes, it is possible, but you must declare the parameters using the previously defined dynamic array type .You are currently using open array.

type
TStrDynArray = array of string;

procedure DoSomeThing(in_cmd:string ; var out_strs: TStrDynArray);
begin
SetLength(out_strs, 2 * Length(inn_cmd));
...

Or, use TArray< string> if Generics available (from Delphi 2009)

procedure DoSomeThing(in_cmd:string; var out_strs: TArray);
begin
SetLength (out_strs, 2 * Length(inn_cmd));

The reason is that the x array in one context is different from the x array in another context.

In the type declaration

type
TDynamicArray = array of string;

It is a dynamic array type, which can be resized. Dynamic arrays were introduced in Delphi 4.

In the parameter definition

procedure Test(var X: array of string);

is open array parameter which means it will accept different The size of the array is used as input. It only needs a pointer (i.e. reference) to the first element, and the index of the last element in the array. It does not know or care whether the input array is a dynamic array or a static array, so it is not allowed to adjust the length .

Open the array parameter earlier than Delphi 1.

Further reading
http://rvelthuis.de/articles/articles-openarr.html

I am trying to pass some strings to a dynamic string array, in this case it is working:

procedure DoSomeThing(in_cmd: string; var out_strs: array of string);
begin
..
for n := low(out_strs) to high(out_strs) do
begin
out_strs[n] :='bla bla';
end;
end;

Use as in the application:

.
.
.
SetLength(my_out_str, sizer);

DoSomeThing('do it now', my_out_str);
.
.

But I want to use the SetLength of my_out_str in the program.
Is it possible?

Yes, it is possible, but you must declare the parameters using the previously defined dynamic array type. You are currently using open array.

< /p>

type
TStrDynArray = array of string;

procedure DoSomeThing(in_cmd:string; var out_strs: TStrDynArray);
begin
SetLength(out_strs, 2 * Length(inn_cmd));
...

Or, use TArray< string>if generics are available (from Delphi 2009)

procedure DoSomeThing(in_cmd:string; var out_strs: TArray);
begin
SetLength(out_strs, 2 * Length(inn_cmd));< /pre>

The reason is that the x array in one context is different from the x array in another context.

In the type declaration

type 
TDynamicArray = array of string;

It is a dynamic array type and can be resized. Dynamic arrays were introduced in Delphi 4.

In the parameter definition

< p>

procedure Test(var X: array of string);

is an open array parameter which means it will accept arrays of different sizes as input. It only needs a pointer ( (I.e. reference) to the first element and the index of the last element in the array. It does not know or care whether the input array is a dynamic array or a static array, so it is not allowed to adjust the length.

Open the array parameter early In Delphi 1.

Further reading
http://rvelthuis.de/articles/articles-openarr.html

Leave a Comment

Your email address will not be published.