1

Background:

I have two forms. An array is created on form1 and it needs to get passed to form2 for procedureX. The array is an array of array of string and is dynamic. The array is used in procedureX and then passed back out. Decleared like the following:

procedure ProcedureX (var multidimensionalarray:array of array of string);

Error:

I know this is going to bring an error because you can't pass multidimensional arrays. The error you get is: E2029 Identifier expected but 'Array' found. From this website: https://blog.spreendigital.de/2016/08/01/pass-a-multidimensional-array-as-a-parameter-with-a-hidden-caveat/ I know you need to create a type.

But I have questions because of the background of my problem: - would I create the type in form1 or form2? - how do you pass a type between forms?

I don't have any code yet because I have no idea where to start. I new to the code and don't really know where to look. I understand this question has been asked many times however because my procedure is on one form and my array on another, I do not know what to do.

3
  • You can declare your types in a separate unit (e.g. MyTypes.pas) and add it to your form units' uses clause to be able to use them (uses ..., MyTypes;). Commented Apr 16, 2018 at 9:31
  • 1
    Great minds... :-) Even on the UNIT name :-) Commented Apr 16, 2018 at 9:33
  • 1
    Depending on your Delphi version, you could also use the generic TArray<TArray<string>> Commented Apr 16, 2018 at 9:37

1 Answer 1

4

I would make the type definition in a 3rd unit - f.ex.:

UNIT MyTypes;

INTERFACE

TYPE
  TStrArr    = ARRAY OF STRING;
  TStrArrArr = ARRAY OF TStrArr;

IMPLEMENTATION

END.

and then USE that unit in both Form1 and Form2's units:

UNIT Main;

INTERFACE

USES ..., MyTypes;

TYPE
  TForm1 = CLASS(TForm)
             .
             .
             .
             PROCEDURE ProcedureX(VAR MultiDimensionalArray : TStrArrArr);
             .
             .
             .
           END;

The idea is to USE the MyTypes unit every place where you need to refer to the multi-dimensional array type (f.ex. declaring a variable of that type, or passing parameters of that type) and not make that unit (MyTypes) dependable on anything else in your project.

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

4 Comments

Works completely! I've ticked your post
In modern Delphi it would be preferable to use TArray<T> rather than declaring bespoke array types
@DavidHeffernan: Not if you want backwards compatibility with previous versions. And since OP didn't specify any Delphi version, I elected to produce a reply that works irrespectively of which Delphi version is used - all the way back to when dynamic arrays were introduced.
Obviously it would be no use if you wanted to support ancient versions of the compiler. But if you wanted to take advantage of modern features like generics, then TArray<T> is clearly preferable.

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.