Se si sa che verrà utilizzato Delphi-7 per il resto della tua vita con la chiavetta (i) fusione TObject. Altrimenti inizia a usare gli oggetti appropriati, questo ti farà risparmiare mal di testa quando eseguirai l'aggiornamento a 64 bit.
Unit uSimpleObjects;
Interface
type
TIntObj = class
private
FI: Integer;
public
property I: Integer Read FI;
constructor Create(IValue: Integer);
end;
type
TDateTimeObject = class(TObject)
private
FDT: TDateTime;
public
property DT: TDateTime Read FDT;
constructor Create(DTValue: TDateTime);
end;
Implementation
{ TIntObj }
constructor TIntObj.Create(IValue: Integer);
begin
Inherited Create;
FI := IValue;
end;
{ TDateTimeObject }
constructor TDateTimeObject.Create(DTValue: TDateTime);
begin
Inherited Create;
FDT := DTValue;
end;
end.
Usage:
var
IO: TIntObj;
SL: TStringList;
bagagli:
SL := TStringList.Create(true); // 'OwnsObjects' for recent Delphi versions
IO := TIntObj.Create(123);
SL.AddObjects(IO);
Recupero:
IO := TIntObj(SL.Objects[4]);
ShowMessage('Integer value: '+ IntToStr(IO.I));
per Delphi-7
TIntObj := TStringList.Create;
e si deve liberare gli oggetti da soli:
for i := 0 to Sl.Count-1 do
begin
IO := TIntObj(SL.Objects[i]);
IO.Free;
end;
SL.Free;
fonte
2013-04-23 07:33:50
Che tipo di oggetto è? Non esiste una funzione generica da TObject a stringa. – Blorgbeard
come nota, ma non una risposta, ti consiglio di creare il tuo oggetto da archiviare in questo campo. Dopotutto, questo è ciò a cui è destinato. Il tuo oggetto potrebbe includere più informazioni se necessario. Ad esempio, hai detto che è un elenco di lingue. Un oggetto (ad esempio 'TLanguage') potrebbe avere proprietà come" Name: String "," ID: Integer "e altro. –
A proposito, stai ancora chiedendo come eseguirlo su una stringa, ma la domanda menziona numero intero. Mantieni la domanda sugli interi memorizzati in un campo TObject e la conversione stringa/intero è la parte facile ('IntToStr',' StrToIntDef', ecc.). Quindi il titolo dovrebbe essere più simile a "Come memorizzare un numero intero in un campo TObject?" –