Egalité de tableaux

Résolu
cs_cantador Messages postés 4720 Date d'inscription dimanche 26 février 2006 Statut Modérateur Dernière intervention 31 juillet 2021 - 11 juil. 2009 à 12:14
cs_cantador Messages postés 4720 Date d'inscription dimanche 26 février 2006 Statut Modérateur Dernière intervention 31 juillet 2021 - 11 juil. 2009 à 19:59
Bonjour,

J'ai besoin de déclarer une égalité de tableaux et je viens de voir un petit souci :

Si mes deux tableaux(tab1, tab2) sont déclarés dans deux unité différentes(avec les uses) alors sur
Tab1 := Tab2->> plantage (types incompatibles) aussi avec Tab1 := Copy(Tab2);

Si les déclarations sont répétées:
Tab1 : array of integer;
Tab2: array of integer;

même constat..

Pour que ça marche, il faut déclarer les deux tableaux au même endroit,
soit en global, soit en local et dans la même unité.

ex :
implementation
{$R *.dfm}


procedure TForm2.Button1Click(Sender: TObject);
var
  Tab1,Tab2: array of integer;
begin
  SetLength(Tab2, 10);
  Tab2[2] := 1028;
  Tab1 := Tab2;
  ShowMessage(IntToStr(Tab1[2]));
end;

Est-ce normal ?

cantador

4 réponses

Cirec Messages postés 3833 Date d'inscription vendredi 23 juillet 2004 Statut Modérateur Dernière intervention 18 septembre 2022 50
11 juil. 2009 à 14:54
je viens de tester et ça fonctionne parfaitement

une unité commune aux deux fiches:

unit UCommonType;

interface

type

  PDynIntArray = ^TDynIntArray;

  TDynIntArray = array of Integer;

implementation

end.


Fiche principale:

unit Unit1;

interface

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, UCommonType, StdCtrls;

type

  TForm1 = class(TForm)

    Button1: TButton;

    Label1: TLabel;

    procedure Button1Click(Sender: TObject);

    procedure FormCreate(Sender: TObject);

  private

  public

  end;

var

  Form1: TForm1;

  IntArray1: TDynIntArray;

implementation

uses Unit2;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);

begin

  Form2.Show;

end;

procedure TForm1.FormCreate(Sender: TObject);

var I: Integer;

begin

  Label1.Caption := EmptyStr;

  Randomize;

  SetLength(IntArray1, 3);

   for I : = 0to 2do

    IntArray1[I] := Random(High(Integer));

   for I : = Low(IntArray1) to High(IntArray1) do

    Label1.Caption := Label1.Caption + IntToStr(IntArray1[I])+ ' ';

end ;

end.


Fiche secondaire:

unit Unit2;

interface

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, UCommonType, StdCtrls;

type

  TForm2 = class(TForm)

    Label1: TLabel;

    procedure FormShow(Sender: TObject);

  private

  public

  end;

var

  Form2: TForm2;

  IntArray2: TDynIntArray;

implementation

{$R *.dfm}
uses Unit1;

procedure TForm2.FormShow(Sender: TObject);

var I: Integer;

begin

  Label1.Caption := EmptyStr;

  IntArray2 := IntArray1;
// <<<----------
  for I : = Low(IntArray2) to High(IntArray2) do

    Label1.Caption := Label1.Caption + IntToStr(IntArray2[I])+ ' ';

end ;

end.


 
@+
Cirec

<hr siz ="" />
3
Cirec Messages postés 3833 Date d'inscription vendredi 23 juillet 2004 Statut Modérateur Dernière intervention 18 septembre 2022 50
11 juil. 2009 à 12:51
Salut,

déclare un "Type":

type
  TIntArray = array of Integer;

et ça devrait passer sans problèmes

 
@+
Cirec

<hr siz="" />
0
cs_cantador Messages postés 4720 Date d'inscription dimanche 26 février 2006 Statut Modérateur Dernière intervention 31 juillet 2021 13
11 juil. 2009 à 14:07
meri cirec pour l'info, mais je l'avais déjà trouvée dans le forum il y a peu de temps dans une discussion (toi ou f0xi je ne sais plus..)
mais après essai, quedal..

Je vais reprendre mon code et je te ferai signe..

cantador
0
cs_cantador Messages postés 4720 Date d'inscription dimanche 26 février 2006 Statut Modérateur Dernière intervention 31 juillet 2021 13
11 juil. 2009 à 19:59
Tout marche bien cirec.
J'avais juste oublier que lorsqu'on déclare un type il faut s'y tenir.

Un grand merci et bonne vacances si tu n'es pas encore parti

cantador
0
Rejoignez-nous