Bonjour,

Au terme de sa discussion d'avril 2007, Dereck07 fournit le code permettant de récupérer un résultat array of string d'une fonction en déclarant un type. Voici son code (qui fonctionne ).
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
type TIntArray = array[-1..1] of integer;
 
function giveT(v:integer):TIntArray ;
var
 t:TIntArray;
begin
 t[-1]:=v;
 t[0]:=v+1;
 t[1]:=v+2;
 result:=t;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
var
  t1:TIntArray;
begin
  t1:=giveT(2);
  memo1.Lines.Add(inttostr(t1[-1])+' - '+inttostr(t1[0])+' - '+inttostr(t1[1]));
end;
Je m'en suis inspiré largement. Mon projet : une fonction partagée installée dans une unité (UnitTool) doit retourner à une fiche (Form1/Unit1) un résultat array of string. Voici mon code :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
unit UnitTool;

interface

uses
  SysUtils, Dialogs;

type
  TTableauChaines = array of string;

function ChargeTableau:TTableauChaines;

implementation

function ChargeTableau:TTableauChaines;
var Tableau:TTableauChaines;
    i:integer;
begin
  for i:=0 to 4 do
    begin
      SetLength(Tableau,i+1);
      Tableau[i]:=IntToStr(i);
      ShowMessage(Tableau[i]);  // <--- pour vérifier le fonctionnement
    end;
  result:=Tableau;
end;

end.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TTableauChaines = array of string;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Déclarations privées }
  public
    { Déclarations publiques }
  end;

var
  Form1: TForm1;

implementation

uses UnitTool;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var MonTableau:TTableauChaines;
begin
  MonTableau:=ChargeTableau; // <--- ici, plantage
  //ChargeTableau;  // <---- ici, ça marche, mais aucun intérêt !!!
end;

end.
A la construction, plantage avec message Types incompatibles ! Pourtant, la variable MonTableau et le résultat de la fonction sont de type TTableauChaines !!!

Cordialement,
Alain