IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Delphi Discussion :

Procédure : Tableau en passage de paramètres


Sujet :

Delphi

  1. #1
    Rédacteur/Modérateur
    Avatar de ero-sennin
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2005
    Messages
    2 965
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2005
    Messages : 2 965
    Points : 4 935
    Points
    4 935
    Par défaut Procédure : Tableau en passage de paramètres
    Bonjour,

    J'ai un soucis concernant le passage de paramètre d'un tableau de type personnalisé.

    Voici sa déclaration :
    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
    //Délcaration de constantes
    const
    NbreMaxRelation=50;
     
    //Déclaration de types personnalisés
    type TRelation=record
      NumEntite1,NumEntite2 : integer;
    end;
     
    procedure RedessinerRelation(TabRelation:TRelation; EntiteSelect:TImage; NumEntite2:integer; NbRelation:integer; ZoneDessin:TImage);
     
    //Déclaration des variables globales
    var
      FPrincipale: TFPrincipale;
      FormeDessinee: String;
      NbreEntite,NbreRelation,XClic,YClic: integer;
      FormeEntite,EntiteADeplacer,EntiteDepart,EntiteArrivee: TImage;
      Clic:boolean;
      Entite1Clic,Entite2Clic:boolean;
      TableauRelation:array[1..NbreMaxRelation] of TRelation;
    implementation
     
    {$R *.dfm}
     
    procedure RedessinerRelation(TabRelation:TRelation; EntiteSelect:TImage; NumEntite2:integer; NbRelation:integer; ZoneDessin:TImage);
    var
    EntiteRelie:TImage;
    i:integer;
    begin
      for i:=1 to NbRelation do
      begin
        if (TabRelation[i].NumEntite1 = 1) then
        begin
          // code ...
        end;
      end;
    end;
    Il me dit comme quoi j'ai une erreur sur TabRelation ....
    J'ai lu que pour passer un tableau en paramètre il faut utiliser le mot clé var mais je ne sais pas comment déclarer ...
    Dois aussi déclarer TabRelation avec le nombre de case qu''il comporte ...?

    Merci de votre aide.

  2. #2
    Membre actif
    Homme Profil pro
    Inscrit en
    Juin 2004
    Messages
    219
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Andorre

    Informations forums :
    Inscription : Juin 2004
    Messages : 219
    Points : 241
    Points
    241
    Par défaut
    Salut

    TabRelation n'est pas un array, c'est un Record.

    Pour declarer un array du record tu dois faire quelque chose comme ça

    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
    //Délcaration de constantes
    const
    NbreMaxRelation=50;
    
    //Déclaration de types personnalisés
    type TRelation=record
      NumEntite1,NumEntite2 : integer;
    end;
    
    type TArrRelation = array[1..10] of TRelation;
    
    procedure RedessinerRelation(var TabRelation:TArrRelation; EntiteSelect:TImage;
       NumEntite2:integer; NbRelation:integer; ZoneDessin:TImage);
    
    implementation
    
    {$R *.dfm}
    
    procedure RedessinerRelation(var TabRelation:TArrRelation; EntiteSelect:TImage; 
      NumEntite2:integer; NbRelation:integer; ZoneDessin:TImage);
    var
    EntiteRelie:TImage;
    i:integer;
    begin
      for i:=1 to NbRelation do
      begin
        if (TabRelation[i].NumEntite1 = 1) then
        begin
          // code ...
        end;
      end;
    end;
    A+

  3. #3
    Expert confirmé
    Avatar de anapurna
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2002
    Messages
    3 444
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Arts - Culture

    Informations forums :
    Inscription : Mai 2002
    Messages : 3 444
    Points : 5 864
    Points
    5 864
    Par défaut
    salut

    tu peut meme faire mieux creer un type

    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 
     TRelation=record
         NumEntite1,NumEntite2 : integer;
      end;
     
      tabRelation : array of TRelation
     
    procedure RedessinerRelation(aTabRelation:TabRelation; EntiteSelect:TImage; NumEntite2:integer; NbRelation:integer; ZoneDessin:TImage);
    var
    EntiteRelie:TImage;
    i:integer;
    begin
      for i:=low(TabRelation) to high(TabRelation) do
      begin
        if (TabRelation[i].NumEntite1 = 1) then
        begin
          // code ...
        end;
      end;
    end;
    @+ Phil

  4. #4
    Rédacteur/Modérateur
    Avatar de ero-sennin
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2005
    Messages
    2 965
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2005
    Messages : 2 965
    Points : 4 935
    Points
    4 935
    Par défaut
    Merci cadetill, tout marche correctement.
    Par contre, avec ton code anapurna, ca ne fonctionne pas.

    Aussi, pourquoi doit-on utiliser le mot var ? Ca sert à quoi exactement car pour les autres variables, on ne le mets pas !

    Merci

  5. #5
    Rédacteur/Modérateur
    Avatar de ero-sennin
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2005
    Messages
    2 965
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2005
    Messages : 2 965
    Points : 4 935
    Points
    4 935
    Par défaut
    Une autre question aussi tant que le topic est ouvert :

    Pourquoi je ne peux pas faire un FindComponent dans ce bout de 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
    procedure RedessinerRelation(var TabRelation:TArrayRelation; EntiteSelect:TImage; NumEntite2:integer; NbRelation:integer; ZoneDessin:TImage);
    var
    EntiteRelie:TImage;
    NbreEntiteConcernee:integer;
    i:integer;
    begin
    for i:=1 to NbRelation do
    begin
     if (TabRelation[i].NumEntite1 = EntiteSelect.Tag)then
     begin
     EntiteRelie:=TImage (FindComponent('Entite'+i);
     end;
    end;
    end;
    Si il vous manque une partie de code, n'hésitez pas, je la posterai
    Merci

  6. #6
    Membre éprouvé Avatar de Yurck
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Février 2005
    Messages
    682
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 15
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2005
    Messages : 682
    Points : 912
    Points
    912
    Par défaut
    FindComponent est une méthode il faut donc savoir dans quoi tu cherche.

    Par exemple sur le fenetre active de ton appli


    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
    procedure RedessinerRelation(var TabRelation:TArrayRelation; EntiteSelect:TImage; NumEntite2:integer; NbRelation:integer; ZoneDessin:TImage);
    var
    EntiteRelie:TImage;
    NbreEntiteConcernee:integer;
    i:integer;
    begin
    for i:=1 to NbRelation do
    begin
     if (TabRelation[i].NumEntite1 = EntiteSelect.Tag)then
     begin
       EntiteRelie:=TImage (Screen.ActiveForm.FindComponent('Entite'+i);
     end;
    end;
    end;
    a+

  7. #7
    Membre éclairé Avatar de slimjoe
    Homme Profil pro
    Inscrit en
    Juin 2005
    Messages
    647
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Canada

    Informations forums :
    Inscription : Juin 2005
    Messages : 647
    Points : 789
    Points
    789
    Par défaut
    Citation Envoyé par ero-sennin

    Aussi, pourquoi doit-on utiliser le mot var ? Ca sert à quoi exactement car pour les autres variables, on ne le mets pas !
    var est le mot clé qui permet d'indiquer à Delphi que le paramètre est passé par référence et non par valeur.


    [Edit]
    Dans mon aide Delphi 7 (désolé c'est en anglais) :

    Most parameters are either value parameters (the default) or variable (var) parameters. Value parameters are passed by value, while variable parameters are passed by reference. To see what this means, consider the following functions.

    function DoubleByValue(X: Integer): Integer; // X is a value parameter
    begin
    X := X * 2;
    Result := X;
    end;
    function DoubleByRef(var X: Integer): Integer; // X is a variable parameter
    begin
    X := X * 2;
    Result := X;
    end;

    These functions return the same result, but only the second one--DoubleByRef--can change the value of a variable passed to it. Suppose we call the functions like this:

    var
    I, J, V, W: Integer;
    begin
    I := 4;
    V := 4;
    J := DoubleByValue(I); // J = 8, I = 4
    W := DoubleByRef(V); // W = 8, V = 8
    end;

    After this code executes, the variable I, which was passed to DoubleByValue, has the same value we initially assigned to it. But the variable V, which was passed to DoubleByRef, has a different value.

    A value parameter acts like a local variable that gets initialized to the value passed in the procedure or function call. If you pass a variable as a value parameter, the procedure or function creates a copy of it; changes made to the copy have no effect on the original variable and are lost when program execution returns to the caller.

    A variable parameter, on the other hand, acts like a pointer rather than a copy. Changes made to the parameter within the body of a function or procedure persist after program execution returns to the caller and the parameter name itself has gone out of scope.

    Even if the same variable is passed in two or more var parameters, no copies are made. This is illustrated in the following example.

    procedure AddOne(var X, Y: Integer);
    begin
    X := X + 1;
    Y := Y + 1;
    end;
    var I: Integer;
    begin
    I := 1;
    AddOne(I, I);
    end;

    After this code executes, the value of I is 3.

    If a routine's declaration specifies a var parameter, you must pass an assignable expression--that is, a variable, typed constant (in the {$J+} state), dereferenced pointer, field, or indexed variable--to the routine when you call it. To use our previous examples, DoubleByRef(7) produces an error, although DoubleByValue(7) is legal.

    Indexes and pointer dereferences passed in var parameters--for example, DoubleByRef(MyArray[I])--are evaluated once, before execution of the routine.

  8. #8
    Rédacteur/Modérateur
    Avatar de ero-sennin
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2005
    Messages
    2 965
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2005
    Messages : 2 965
    Points : 4 935
    Points
    4 935
    Par défaut
    Merci Yurck , cela fonctionne bien !
    Et merci aussi à slimjoe, je regarderai ton post plus en détails

    Bonne Journée.

  9. #9
    Rédacteur/Modérateur
    Avatar de ero-sennin
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2005
    Messages
    2 965
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2005
    Messages : 2 965
    Points : 4 935
    Points
    4 935
    Par défaut
    Dites, encore une question (bé oui lol).

    J'ai créée une procédure dont voici le 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
     
     
    public
    ...
    ...
    procedure TracerRelation(Entite1:TImage; Entite2:TImage; ZoneDessin:TImage);
    end;
     
    var 
    ...
    ...
     
    implementation
     
    {$R *.dfm}
     
    procedure TFPrincipale.TracerRelation(Entite1:TImage; Entite2:TImage; ZoneDessin:TImage);
    var
    CentreEntite1X,CentreEntite1Y,CentreEntite2X,CentreEntite2Y: integer;
    begin
    CentreEntite1X:=Entite1.Left+(Entite1.Width div 2);
    CentreEntite1Y:=Entite1.Top+(Entite1.Height div 2);
    CentreEntite2X:=Entite2.Left+(Entite2.Width div 2);
    CentreEntite2Y:=Entite2.Top+(Entite2.Height div 2);
    ZoneDessin.Canvas.Polyline([Point(CentreEntite1X,CentreEntite1Y),Point(CentreEntite2X,CentreEntite2Y)]);
    end;
    Cette procédure, je voudrais qu'elle soit appelé par une autre procédure :

    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
     
    procedure RedessinerRelation(var TabRelation:TArrayRelation; EntiteSelect:TImage; NumEntite2:integer; NbRelation:integer; ZoneDessin:TImage);
    var
    EntiteRelie:TImage;
    NbreEntiteConcernee:integer;
    i:integer;
    begin
    for i:=1 to NbRelation do
    begin
     if (TabRelation[i].NumEntite1 = EntiteSelect.Tag)then
     begin
     EntiteRelie:=TImage (FPrincipale.FindComponent('Entite'+IntToStr(i)));
     TracerRelation(EntiteDepart,EntiteArrivee,BitmapDessin);
     end;
    end;
    end;
    Seul hic, c'est qu'il ne reconnait pas la procédure TracerRelation.
    Merci

  10. #10
    Membre éprouvé Avatar de Yurck
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Février 2005
    Messages
    682
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 15
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2005
    Messages : 682
    Points : 912
    Points
    912
    Par défaut
    Oh !
    Et si tu mettais ta procédure dans ta form.
    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
    procedure TFPrincipale.RedessinerRelation(var TabRelation:TArrayRelation; EntiteSelect:TImage; NumEntite2:integer; NbRelation:integer; ZoneDessin:TImage);
    var
    EntiteRelie:TImage;
    NbreEntiteConcernee:integer;
    i:integer;
    begin
    for i:=1 to NbRelation do
    begin
     if (TabRelation[i].NumEntite1 = EntiteSelect.Tag)then
     begin
     EntiteRelie:=TImage (FPrincipale.FindComponent('Entite'+IntToStr(i)));
     TracerRelation(EntiteDepart,EntiteArrivee,BitmapDessin);
     end;
    end;
    end;
    C'est un problème de visibilité.
    Une méthode n'est connu que de son conteneur (objet).
    Tu dois donc toujours la pointée.

  11. #11
    Rédacteur/Modérateur
    Avatar de ero-sennin
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2005
    Messages
    2 965
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2005
    Messages : 2 965
    Points : 4 935
    Points
    4 935
    Par défaut
    Oops, je me suis trompé dans le 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
    public
    ...
    ...
    procedure TracerRelation(Entite1:TImage; Entite2:TImage; ZoneDessin:TImage);
    end;
    
    procedure RedessinerRelation(var TabRelation:TArrayRelation; EntiteSelect:TImage; NumEntite2:integer; NbRelation:integer; ZoneDessin:TImage);
    
    var 
    ...
    ...
    
    implementation
    
    {$R *.dfm}
    
    procedure TFPrincipale.TracerRelation(Entite1:TImage; Entite2:TImage; ZoneDessin:TImage);
    var
    CentreEntite1X,CentreEntite1Y,CentreEntite2X,CentreEntite2Y: integer;
    begin
    CentreEntite1X:=Entite1.Left+(Entite1.Width div 2);
    CentreEntite1Y:=Entite1.Top+(Entite1.Height div 2);
    CentreEntite2X:=Entite2.Left+(Entite2.Width div 2);
    CentreEntite2Y:=Entite2.Top+(Entite2.Height div 2);
    ZoneDessin.Canvas.Polyline([Point(CentreEntite1X,CentreEntite1Y),Point(CentreEntite2X,CentreEntite2Y)]);
    end;
    La déclaration de ma procédure ne se fait pas dans la partie public, mais juste après.

    En plus clair, ca donne cela :
    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
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
      public
        { Déclarations publiques }
        procedure EntiteOnDblClick(Sender: TObject);
        procedure EntiteOnMouseMove(Sender: TObject; Shift: TShiftState;
          X, Y: Integer);
        procedure EntiteOnMouseDown(Sender: TObject;
          Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
        procedure EntiteOnMouseUp(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
        procedure TracerRelation(Entite1:TImage; Entite2:TImage; ZoneDessin:TImage);
        end;
    //Délcaration de constantes
    const
    NbreMaxRelation=50;
    
    //Déclaration de types personnalisés
    type TRelation=record
      NumEntite1,NumEntite2 : integer;
    end;
    
    type TArrayRelation = array[1..NbreMaxRelation] of TRelation;
    
    procedure RedessinerRelation(var TabRelation:TArrayRelation; EntiteSelect:TImage; NumEntite2:integer; NbRelation:integer; ZoneDessin:TImage);
    
    //Déclaration des variables globales
    var
      FPrincipale: TFPrincipale;
      FormeDessinee: String;
      NbreEntite,NbreRelation,XClic,YClic: integer;
      FormeEntite,EntiteADeplacer,EntiteDepart,EntiteArrivee: TImage;
      Clic:boolean;
      Entite1Clic,Entite2Clic:boolean;
      TableauRelation:array[1..NbreMaxRelation] of TRelation;
    implementation
    
    {$R *.dfm}
    
    procedure TFPrincipale.TracerRelation(Entite1:TImage; Entite2:TImage; ZoneDessin:TImage);
    var
    CentreEntite1X,CentreEntite1Y,CentreEntite2X,CentreEntite2Y: integer;
    begin
    CentreEntite1X:=Entite1.Left+(Entite1.Width div 2);
    CentreEntite1Y:=Entite1.Top+(Entite1.Height div 2);
    CentreEntite2X:=Entite2.Left+(Entite2.Width div 2);
    CentreEntite2Y:=Entite2.Top+(Entite2.Height div 2);
    ZoneDessin.Canvas.Polyline([Point(CentreEntite1X,CentreEntite1Y),Point(CentreEntite2X,CentreEntite2Y)]);
    end;
    
    procedure RedessinerRelation(var TabRelation:TArrayRelation; EntiteSelect:TImage; NumEntite2:integer; NbRelation:integer; ZoneDessin:TImage);
    var
    EntiteRelie:TImage;
    NbreEntiteConcernee:integer;
    i:integer;
    begin
    for i:=1 to NbRelation do
    begin
     if (TabRelation[i].NumEntite1 = EntiteSelect.Tag)then
     begin
     EntiteRelie:=TImage (FPrincipale.FindComponent('Entite'+IntToStr(i)));
     FPrincipale.TracerRelation(EntiteDepart,EntiteArrivee,BitmapDessin);
     end;
    end;
    end;
    Voilà
    Bon, normalement cela devrait fonctionner maintenant
    Merci encore de m'avoir éclairé !

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 1
    Dernier message: 07/05/2014, 14h26
  2. Réponses: 6
    Dernier message: 20/01/2013, 14h08
  3. Problème Procédure stockée et passage de paramètre
    Par Ambel1986 dans le forum Développement
    Réponses: 5
    Dernier message: 08/02/2011, 15h04
  4. Réponses: 3
    Dernier message: 03/07/2008, 20h52
  5. Réponses: 3
    Dernier message: 23/03/2005, 11h28

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo