Erreur syntaxe Case Of

Résolu
CharlEm80 Messages postés 66 Date d'inscription vendredi 1 septembre 2006 Statut Membre Dernière intervention 1 août 2012 - 26 avril 2012 à 15:32
CharlEm80 Messages postés 66 Date d'inscription vendredi 1 septembre 2006 Statut Membre Dernière intervention 1 août 2012 - 7 mai 2012 à 14:53
Ca y est après avoir découvert l'interface graphique, j'essaie un peu de code et comme tout débutant ... frustration!

Voici le code

unit Unit8;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  QuickRpt, Qrctrls, Db, DBTables, ExtCtrls,IniFiles;

type
  TForm4 = class(TForm)
    QuickRep1: TQuickRep;
    DataSource1: TDataSource;
    Table1: TTable;
    DataSource2: TDataSource;
    Table2: TTable;
    QRSubDetail1: TQRSubDetail;
    ColumnHeaderBand1: TQRBand;
    DetailBand1: TQRBand;
    QRDBText1: TQRDBText;
    QRBand1: TQRBand;
    QRDBText2: TQRDBText;
    QRDBText3: TQRDBText;
    QRLabel1: TQRLabel;
    QRSysData1: TQRSysData;
    QRDBText4: TQRDBText;
    QRDBText5: TQRDBText;
    QRLabel2: TQRLabel;
    QRLabel3: TQRLabel;
    Table3: TTable;
    DataSource3: TDataSource;
    QRDBText6: TQRDBText;
    QRDBText7: TQRDBText;
    QRDBText8: TQRDBText;
    procedure FormActivate(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
  index_voulu:longint;
    { Public declarations }
  end;

var
  Form4: TForm4;

implementation

{$R *.DFM}


procedure TForm4.FormActivate(Sender: TObject);

begin
table2.filter:='Index='+inttostr(index_voulu);
table2.filtered:=true;
end;

procedure TForm4.FormCreate(Sender: TObject);
var zO_FichierIni:Tinifile;
    zS_Service:Char;
begin
        {Modification - xxx - 22/02/2010: Bug 2634}
        zO_FichierIni := Tinifile.Create(ExtractFilePath(Application.ExeName)+'USI.ini');
        {Lecture dans fichier ini}
        zS_Service := zO_FichierIni.ReadString('USI_PARAMETRES','Unit','')[1];
        zO_FichierIni.free;
        if UpCase(zS_Service)<>'' then
                Qrlabel1.caption:='Relevé contradictoire des quantités de stupéfiants en stock - Service ' + zS_Service
        else
                Qrlabel1.caption:='Relevé contradictoire des quantités de stupéfiants en stock - ERREUR SERVICE';
        end;
[b]        {<<< Ajout - MOI - 26/04/2012: Bug 4530}
        QRDBText6.Transparent := True;
        QRDBText7.Transparent := True;
        QRDBText8.Transparent := True;
        Case UpCase(zS_Service) of
        'A' :   QRDBText6.Transparent := False;
        'B' :   QRDBText7.Transparent := False;
        'D' :   QRDBText8.Transparent := False;
        else
                QRDBText6.Caption := 'Erreur';
                QRDBText6.Transparent := False;
        end;
        {>>> Ajout - MOI - 26/04/2012: Bug 4530}/b
end.


Quand je fais PLAY il me dit :
[Error] Unit8.pas(73): Declaration expected but 'CASE' found
[Error] Unit8.pas(87): '.' expected but ';' found
[Fatal Error] stup.drp(15): Could not compile used unit 'Unit8.pas'

Gosso modo je m'y perd dans les ";" quand les mettres, quand ne pas les mettre?

A la fin d'un IF THEN ELSE il n'y a pas de END IF mais le END; c'est le END IF ou le END de la fonction?

Merci d'avance

8 réponses

Cirec Messages postés 3833 Date d'inscription vendredi 23 juillet 2004 Statut Modérateur Dernière intervention 18 septembre 2022 50
26 avril 2012 à 15:44
Salut,

dans un case of après le else il est attendu qu'une seule instruction.

si tu en as plusieurs il faut les encadrer dans un bloc begin end
        else
          begin
            QRDBText6.Caption := 'Erreur';
            QRDBText6.Transparent := False;
          end;
        end;


je pense que ça devrait fonctionner comme ceci


[hr]@+Cirec
[hr]
3
Cirec Messages postés 3833 Date d'inscription vendredi 23 juillet 2004 Statut Modérateur Dernière intervention 18 septembre 2022 50
26 avril 2012 à 17:07
effectivement
le premier end n'est pas bon !!
et il en manquait un à la fin ...
et tant qu'à faire j'y ai apporté d'autre modification ...
[hr]procedure TForm4.FormCreate(Sender: TObject);

[b]var
  /bzS_Service: Char;
[b]begin
  /b{Modification - xxx - 22/02/2010: Bug 2634}
  {Modification permettant de s'affranchir d'une variable de type TIniFile}
  {le tout en toute sécurité}
  with Tinifile.Create(ExtractFilePath(Application.ExeName) +  'USI.ini') [b]do
  try
    /b{Lecture dans fichier ini}
    {et récupération du premier caractère en majuscule}
    zS_Service :=  UpCase(ReadString('USI_PARAMETRES', 'Unit', '')[1]);
  [b]finally
    /b {ici on libère l'objet même en cas d'erreur}
    free;
   end ;
  if zS_Service <> '' [b]then
    /bQrlabel1.caption : = 'Relevé contradictoire des quantités de stupéfiants en stock - Service ' + zS_Service
  [b]else
    /bQrlabel1.caption :=  'Relevé contradictoire des quantités de stupéfiants en stock - ERREUR SERVICE';
   {<<< Ajout - MOI - 26/04/2012: Bug 4530}
  QRDBText6.Transparent : = True;
  QRDBText7.Transparent := True;
  QRDBText8.Transparent := True;
   case  zS_Service [b]of
    /b'A': QRDBText6.Transparent : = False;
    'B': QRDBText7.Transparent := False;
    'D': QRDBText8.Transparent := False;
  [b]else
    begin
      /bQRDBText6.Caption := 'Erreur';
      QRDBText6.Transparent := False;
    end;
  end; [i]{Ce END; ci c'est le end du CASE OF mais il va peut être avec le begin tout au dessus???}
  /i{>>> Ajout - MOI - 26/04/2012: Bug 4530}
end;
end.
[hr]
voilà cette fois ce code devrait fonctionner.

je te conseille un copier/coller pour ne rien oublier


[hr]@+Cirec
[hr]
3
beckerich Messages postés 302 Date d'inscription jeudi 29 septembre 2005 Statut Membre Dernière intervention 17 septembre 2013 2
27 avril 2012 à 08:46
Bonjour,

pour éviter les bugs lors de modification dans un code, je code systématiquement les blocs comme ceci :

if ... then
begin
  // instruction
end;

if ... then
begin
  // instruction
  // instruction
end
else
  // instruction
begin
end;


et surtout bien indenter, cela aide à trouver les bugs.

Luc.
3
beckerich Messages postés 302 Date d'inscription jeudi 29 septembre 2005 Statut Membre Dernière intervention 17 septembre 2013 2
27 avril 2012 à 08:48
oups,

faute de copier/coller,

if ... then
begin
  // instruction
  // instruction
end
else
begin
  // instruction
end;
3

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
CharlEm80 Messages postés 66 Date d'inscription vendredi 1 septembre 2006 Statut Membre Dernière intervention 1 août 2012 1
26 avril 2012 à 16:25
Ha ok, je sens qu'on progresse mais le compilateur me renvoi une nouvelle erreur que voici

procedure TForm4.FormCreate(Sender: TObject);
var zO_FichierIni:Tinifile;
    zS_Service:Char;
begin
        {Modification - xxx - 22/02/2010: Bug 2634}
        zO_FichierIni := Tinifile.Create(ExtractFilePath(Application.ExeName)+'USI.ini');
        {Lecture dans fichier ini}
        zS_Service := zO_FichierIni.ReadString('USI_PARAMETRES','Unit','')[1];
        zO_FichierIni.free;
        if UpCase(zS_Service)<>'' then
                Qrlabel1.caption:='Relevé contradictoire des quantités de stupéfiants en stock - Service ' + zS_Service
        else
                Qrlabel1.caption:='Relevé contradictoire des quantités de stupéfiants en stock - ERREUR SERVICE';
        end; {fallait il bien mettre un END; ici???}
        {<<< Ajout - MOI - 26/04/2012: Bug 4530}
        QRDBText6.Transparent := True;
        QRDBText7.Transparent := True;
        QRDBText8.Transparent := True;
        Case UpCase(zS_Service) of
        'A' :   QRDBText6.Transparent := False;
        'B' :   QRDBText7.Transparent := False;
        'D' :   QRDBText8.Transparent := False;
        else
                begin
                        QRDBText6.Caption := 'Erreur';
                        QRDBText6.Transparent := False;
                end;
        end; {Ce END; ci c'est le end du CASE OF mais il va peut être avec le begin tout au dessus???}
        {>>> Ajout - MOI - 26/04/2012: Bug 4530}
end.
0
CharlEm80 Messages postés 66 Date d'inscription vendredi 1 septembre 2006 Statut Membre Dernière intervention 1 août 2012 1
26 avril 2012 à 16:47
Oui et du coup le compilateur dit ceci:

[Error] Unit8.pas(73): Declaration expected but identifier 'QRDBText6' found
QRDBText6.Transparent := True;


[Error] Unit8.pas(82): Undeclared identifier: 'QRDBText6'
QRDBText6.Caption := 'Erreur';


[Error] Unit8.pas(83): Missing operator or semicolon
QRDBText6.Transparent := False;


[Error] Unit8.pas(83): Left side cannot be assigned to
QRDBText6.Transparent := False;


[Error] Unit8.pas(84): '.' expected but ';' found
end;
0
CharlEm80 Messages postés 66 Date d'inscription vendredi 1 septembre 2006 Statut Membre Dernière intervention 1 août 2012 1
7 mai 2012 à 11:02
Super, un tout grand merci à tous les deux pour m'avoir aidé à comprendre cette subtilité de Delphi.

Reste un truc avec ce bout de code, comment cacher un des QRDBText à l'affichage (ce que je tente de faire dans mon case of).
J'ai essayé .Transparent, .hide, .visible mais rien ne fonctionne.

Une idée?
0
CharlEm80 Messages postés 66 Date d'inscription vendredi 1 septembre 2006 Statut Membre Dernière intervention 1 août 2012 1
7 mai 2012 à 14:53
trouvé c'était .enabled
0
Rejoignez-nous