codice:
//Definizione delle classi
TFormato = class
constructor create;
private
nome:string;
codice:string;
codice_man:string;
codice_cd:string;
alt:double;
larg:double;
quality:double;
rapW:double;
rapH:double;
codici: array of string;
limiti: array of string;
variabile:boolean;
protected
procedure azzera;
procedure leggi(nomeFormato,nf:string);
public
property getnome: string read nome;
function getcodice: string;
property getcodice_man: string read codice_man;
property getcodice_cd: string read codice_cd;
property getAlt: double read alt;
property getLarg: double read larg;
property getQuality: double read quality;
property getRapW: double read rapW;
property getRapH: double read rapH;
function trovacodice(cod,formato:string):boolean;
function getCodicePerSviluppo(sviluppo:double):string;
property isVariabile: boolean read variabile;
end;
TListaFormati = class
constructor create(nf:string);
private
nf_config:string;
numero_formati:integer;
formati: array of TFormato;
public
function getFormatoByName(nome:string):TFormato;
function getFormatoByID(id:integer):TFormato;
function getFormatoName(id:integer):string;
property getnumeroFormati: integer read numero_formati;
end;
..
//funzioni
procedure TFormato.azzera;
begin
nome:='';
codice:='';
codice_man:='';
codice_cd:='';
alt:=0;
larg:=0;
quality:=0;
rapW:=0;
rapH:=0;
SetLength(limiti,0);
SetLength(codici,0);
variabile:=false;
end;
constructor TFormato.create;
begin
azzera;
end;
function TFormato.getcodice: string;
begin
result:='';
try
if variabile then
result:=codici[0]
else
result:=codice;
except
end;
end;
procedure TFormato.leggi(nomeFormato,nf:string);
var
ini:TiniFile;
l,i:integer;
lim,cod:string;
begin
azzera;
if nomeFormato<>'' then
begin
ini:=TIniFile.Create(nf);
nome:=nomeFormato;
quality:=ini.ReadInteger(nomeFormato,'Quality',0);
rapW:=ini.readFloat(nomeFormato,'Rapporto W',0);
rapH:=ini.readFloat(nomeFormato,'Rapporto H',0);
alt:=ini.readFloat(nomeFormato,'H',0);
larg:=ini.readFloat(nomeFormato,'W',0);
codice:=ini.readstring(nomeFormato,'codice','');
codice_man:=ini.readstring(nomeFormato,'codice manuale','');
codice_cd:=ini.readstring(nomeFormato,'codice automatico','');
variabile:=ini.readstring(nomeFormato,'Tipo','F')='V';
l:=ini.readInteger(nomeFormato,'Limiti',0);
SetLength(limiti,l);
SetLength(codici,l);
if variabile then
begin
for i:=1 to l do
begin
limiti[i-1]:=ini.readstring(nomeFormato,'Limite'+inttoStr(i),'');
codici[i-1]:=ini.readstring(nomeFormato,'Codice'+inttoStr(i),'');
end;
end;
ini.free;
end;
end;
function TFormato.trovacodice(cod,formato:string):boolean;
var
i:integer;
begin
result:=false;
if formato=nome then
begin
if (cod=codice) or (cod=codice_man) or (cod=codice_cd) then
result:=true;
if not result then
begin
for i:=0 to length(codici)-1 do
begin
if codici[i]= cod then
begin
result:=true;
break;
end;
end;
end;
end;
end;
function TFormato.getCodicePerSviluppo(sviluppo:double):string;
var
i:integer;
l:double;
begin
result:='';
for i:=0 to length(limiti)-1 do
begin
l:=strToFloatDef(limiti[i],-1);
if l=-1 then
begin
result:=codici[i];
break;
end
else begin
if sviluppo<=l then
begin
result:=codici[i];
break;
end;
end;
end;
end;
constructor TListaFormati.create(nf:String);
var
ini:TiniFile;
lf:Tstringlist;
i:integer;
begin
nf_config:=nf;
lf:=TStringList.create;
ini:=TIniFile.Create(nf);
ini.ReadSections(lf);
ini.Free;
numero_formati:=lf.Count;
SetLength(formati,numero_formati);
for i:=0 to numero_formati-1 do
begin
formati[i]:=TFormato.create;
formati[i].leggi(lf[i],nf);
end;
lf.free;
end;
function TListaFormati.getFormatoByName(nome:string):TFormato;
var
i:integer;
ris:TFormato;
begin
ris:=TFormato.create;
for i:=0 to numero_formati-1 do
begin
if nome=formati[i].getnome then
begin
ris:=formati[i];
break;
end;
end;
Result:=ris;
end;
function TListaFormati.getFormatoName(id:integer):string;
begin
try
result:=Formati[id].getnome;
except
result:='';
end;
end;
function TListaFormati.getFormatoByID(id:integer):TFormato;
var
ris:TFormato;
begin
ris:=TFormato.create;
if numero_formati<id then
ris:=Formati[id];
result:=ris;
end;
Ok ora se provo a fare questo:
codice:
var
lista:TlistaFormati;
formato1,formato2:TFormato;
begin
lista:=TlistaFormati.create('formati.ini');
formato1:=TFormato.create;
formato1:=lista.getFormatoByName('PICCOLO');
showmessage(formato1.getnome);
formato1.free;
formato2:=TFormato.create;
formato2:=lista.getFormatoByName('PICCOLO');
showmessage(formato2.getnome);
formato2.free;
end;
Il risultato che ottengo del primo showmessage è "PICCOLO" mentre del secondo è ""... l'elemento "formati" dell'oggetto "lista" si è cancellato insieme alla variabile "formato1"!
Inpratica quando ho assegnato a fomato1 il risultato della funzinoe "lista.getFormatoByName" gli ha passato l'indirizzo a quella variabile e non il valore!
mi chiedo è normale? perché accade ciò?