0

I have a unit in which there are multiple variables that has to be same sized vectors.

However I do not know the length of this array before i parse the file.

So I want to have a dynamic array that is "global" for the whole unit and then i can

The code below shows the issue as well as the solution I have now. The solution I have now is to assign a maximum value to be the length of the array.

unit xyz;
interface 

uses
abc

const
maxval=50;

type
vectorofdouble = [1...maxval] of double;  // I want to change this to dynamic array

type
  T_xyz = object

  public
    NP: integer;
  private
    var1: vectorofdouble;        
    var2: vectorofdouble;        
   public
    number: integer;       
    var3: vectorofdouble; 

  private
    procedure Create();
    function func1(etc): integer;
  public
    procedure ReadFile(const FileName, inputs: string);
  end;

implementation
procedure T_xyz.ReadFile();
////////
Read(F,np)
  //SetLength(vectorofdouble, np) // DOES NOT WORK
  for i := 0 to maxval // I DONT WANT TO LOOP UP TO MAXVAL
  begin
    var1[i] := 0
  end;

procedure T_xyz.func1(etc);
////////
do stuff
  for i := 0 to maxval // I DONT WANT TO LOOP UP TO MAXVAL
  begin
    var2[i] := 0
  end;
end;

end.
19
  • @Brian as soon as i change from vectorofdouble = [1...maxval] of double; to vectorofdouble= array of double; i can not really access to vectorofdouble any where in the code setlength gives error Commented Jan 30, 2020 at 13:48
  • You need to pass the array rather than the type to SetLength. So, for instance, SetLength(var1, ...). By the way, what does OFlex stand for? Commented Jan 30, 2020 at 14:00
  • Delphi 10.3 Rio. If I use "vectorofdouble= array of double;" under the type (see above) and f.e add a SetLength(vectorofdouble,10); in any function [dcc32 Error] filename.pas(438): E2029 '(' expected but ',' found Commented Jan 30, 2020 at 14:02
  • 1
    My comment explains what is wrong Commented Jan 30, 2020 at 14:03
  • @DavidHeffernan it is the main code i deal with the example above is just a dummy. SetLength(var1, ...) works fine however i have 30 variables that "inherit" from vectorofdouble var1, var2 varxxx. The code above allows it to be allocated once instead of for each variable is there a method Commented Jan 30, 2020 at 14:05

2 Answers 2

3

You want to use a dynamic array instead of a fixed-length array. You do that by using

array of <Type>

instead of

array[<Low>..<High>] of <Type>

Then SetLength() will work, but you need to pass it a dynamic array variable instead of a type.

Try this:

unit xyz;

interface

uses
  abc;

type
  vectorofdouble = array of double;

type
  T_xyz = object
  public
    NP: integer;
  private
    var1: vectorofdouble;
    var2: vectorofdouble;
  public
    number: integer;
    var3: vectorofdouble;
  private
    procedure Create();
    function func1(etc): integer;
  public
    procedure ReadFile(const FileName, inputs: string);
  end;

implementation

procedure T_xyz.ReadFile();
var
  i: integer;
begin
  Read(F, NP);
  SetLength(var1, NP);
  for i := 0 to NP-1 do
  begin
    var1[i] := 0;
  end;
end;

procedure T_xyz.func1(etc);
begin
  for i := Low(var2) to High(var2) do
  begin
    var2[i] := 0;
  end;
end;

end.
Sign up to request clarification or add additional context in comments.

1 Comment

i am not sure to answer this answer because : var2 is missing a setlength(var2,Np) argument which can be misleading as if it is enough to allocate var1 only.
-1

You must pass the array to SetLength rather than the type. So instead of

SetLength(vectorofdouble, np)

you must use

SetLength(var1, np)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.