cs_manudel
Messages postés63Date d'inscriptionlundi 29 avril 2002StatutMembreDernière intervention 7 mars 2005
-
22 mai 2002 à 12:30
cs_Delphiprog
Messages postés4297Date d'inscriptionsamedi 19 janvier 2002StatutMembreDernière intervention 9 janvier 2013
-
22 mai 2002 à 19:53
Je déclare une fonction qui renvoit un résultat de type TPersonne, et je voudrais savoir
comment faire pour la mettre au même nivau de visibilité que les autres procédures et
fonctions dans mon unité.
[voir le code en dessous]
Mon problème est que je veux appeller la fonction I2C dans la fonction GenerePersonneAlea,
la fonction I2C n'est pas reconnue.
Comment y remédier ?
type
TfmFichierSequentiel = class(TForm)
function I2C (const i: Integer): String;
private
public
end;
type
TPersonne = record
sNom,
sPrenom : String[100];
wAge : Word;
bHomme : Boolean;
end;
TPersFich = file of TPersonne;
var
fmFichierSequentiel: TfmFichierSequentiel;
implementation
{$R *.dfm}
function GenerePersonneAlea : TPersonne;
begin
//
end;
function TfmFichierSequentiel.I2C (const i: Integer): String;
begin
//
end;
cs_Delphiprog
Messages postés4297Date d'inscriptionsamedi 19 janvier 2002StatutMembreDernière intervention 9 janvier 201332 22 mai 2002 à 13:51
D'après la déclaration, la fonction I2C est membre de la classe TfmFichierSequentiel.
Pour y accéder en dehors de cette classe, il faut donc préfixer l'appel à cette fonction par le nom de l'instance de la classe TfmFichierSequentiel soit FrmFichierSequentiel :
cs_Delphiprog
Messages postés4297Date d'inscriptionsamedi 19 janvier 2002StatutMembreDernière intervention 9 janvier 201332 22 mai 2002 à 19:53
Est-ce bien un problème de portée de fonction ?
Look at this :
type
TFrmFichierSequentiel = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Déclarations privées }
public
{ Déclarations publiques }
function I2C(const i: Integer): string;
end;
TPersonne = record
sNom, sPrenom: string[100];
wAge: Word;
bHomme: Boolean;
end;
TPersFich = file of TPersonne;
var
FrmFichierSequentiel: TFrmFichierSequentiel;
implementation
{$R *.DFM}
function GenerePersonneAlea: TPersonne;
var
Foo : String;
Personne : TPersonne;
F1 : TPersFich;
begin
//
Foo := FrmFichierSequentiel.I2C(2345);
with Personne do
begin
sNom := Foo;
sPrenom := Foo;
wAge := 50;
bHomme := True;
end;
//Ecriture dans un fichier
AssignFile(F1, 'Test.dat');
Rewrite(F1);
Write(F1, Personne);
CloseFile(F1);
//Vérif lecture du fichier
AssignFile(F1, 'Test.dat');
Reset(F1);
Read(F1, Personne);
CloseFile(F1);
Result := Personne;
end;
{ TFrmFichierSequentiel }
function TFrmFichierSequentiel.I2C(const i: Integer): string;
begin
Result := IntToStr(i);
end;
procedure TFrmFichierSequentiel.Button1Click(Sender: TObject);
var
APersonne : TPersonne;
begin
APersonne := GenerePersonneAlea;
ShowMessage(APersonne.sNom);
end;
Aucun problème de portée alors que nous avons les mêmes déclarations !