Comment utilisé une chaine comme un nom de variable sous delphi.
Exp (pour d'autre lang)
(#"abc"):=2 // Affecte le variable abc:=2
Version imprimable
Comment utilisé une chaine comme un nom de variable sous delphi.
Exp (pour d'autre lang)
(#"abc"):=2 // Affecte le variable abc:=2
Non ça n'est pas possible, le nom des variables ne survit pas à la compilation. Il faut gérer ça toi même, avec un TStringList par exemple ou faire autrement, car à l'exception des composants qu'il peut être intéressant de retrouver par leur nom (voir méthode findComponent), ça n'est pas terrible de programmer comme ça.
Bloon
Merci Bloon pour ta réponse. Mais je crois qu'il ya un mal entendu.
un exemple:
Supposon que dans mon programme je travaille avec une table (DB)
où il y'à plusieurs champs de même type (Varchar)
Champ1: Fourn_id_1
Champ2: Fourn_id_2
.
.
mon objectif est de traiter l'évenement onchange de ces champs dans une seule procedure (au lieu d'une proc pour chaque champ).
{illustration}
procedure TF_prod_tab.detailFOURN_ID1Change(Sender: TField);
Var
Champ1,champ2,champ3,champ4 :String;
Index : String;
begin
index:=Rightstr(TFIBBCDField(Tfield).FieldName,1);
Champ1:='detailFOURN_ID'+index+'.AsInt64';
Champ2:='detailCOND'+index+'.AsInt64';
Champ3:='detailART_LIB'+index+'.AsString';
Champ4:='detailCOEF'+index+'.AsFloat';
if (#Champ1)<>0 then begin
(#Champ2) :=DetailUNITE_GEST.AsInt64;
(#Champ3):=detailART_LIB.AsString;
(#Champ4):=1;
end
else begin
(#Champ2) :=0;
(#Champ3):='';
(#Champ3):=0;
end;
end;
ce n'est pas plus simple comme ca ?
Merci
Dans un sens, tu as la propriété Name pour tous les composants et controles dérivés de TControl.
Dans l'autre sens, tu as, à partir du parent du controle auquel tu souhaite acceder, la méthode :
FindChildControl(const ControlName:string):TControl;
Ca devrait marcher avec deset desCode:is
.Code:as
Sinon,pour les Tables et les Query, tu as :
TTable.FieldByName(NomDuChamp:string).asString (AsBoolean,AsInteger,...)
Dans ton cas, on aurait :
Enfin moi, je fais comme ça...Code:
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 procedure TF_prod_tab.detailFOURN_ID1Change(Sender: TField); Var Champ1,champ2,champ3,champ4 :String; Index : String; begin index:=Rightstr(TFIBBCDField(Tfield).FieldName,1); Champ1:='detailFOURN_ID'+index; Champ2:='detailCOND'+index; Champ3:='detailART_LIB'+index; Champ4:='detailCOEF'+index; with TF_prod_tab do begin Edit; if FielByName (Champ1).AsInt64r<>0 then begin FieldByName (Champ2).AsInt64 :=DetailUNITE_GEST.AsInt64; FieldByName (Champ3).AsString:=detailART_LIB.AsString; FieldByName (Champ4).AsFloat:=1; end else begin FieldByName (Champ2).AsInt64 :=0; FieldByName (Champ3).AsString:=''; FieldByName (Champ4).AsFloat:=0; end; Post; end; end;