Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store a Integer in a TObject property and then show that value to the user?

Of course, this piece of code will not compile. First I need to cast a TObject value to Integer. Then, read it as a string. What function should I use?

for i := 1 to 9 do begin
    cmbLanguage.Items.AddObject(Format('Item %d', [i]), TObject(i));
end;

cmbLanguage.ItemIndex := 2;

ShowMessage(cmbLanguage.Items.Objects[cmbLanguage.ItemIndex]);

Or maybe it's possible to use String instead of Integer in the first place?

like image 328
Mikhail Avatar asked Apr 23 '13 00:04

Mikhail


People also ask

Can an object property be a number?

Can JavaScript object property be a number? Against what many think, JavaScript object keys cannot be Number, Boolean, Null, or Undefined type values. Object keys can only be strings, and even though a developer can use other data types to set an object key, JavaScript automatically converts keys to a string a value.

How do you parse an integer to an object in Java?

If your object is a String , then you can use the Integer. valueOf() method to convert it into a simple int : int i = Integer. valueOf((String) object);

Which of the following converts an int to an Integer object?

Use Integer constructor to convert int primitive type to Integer object.


2 Answers

cmbLanguage.Items.AddObject(Format('Item %d', [i]), TObject(i));

Here you are adding an item with an "object" which is actually an integer (i) casted to a TObject.

Since you are actually storing an int in the object field, you can just cast it back to Integer, then convert that to a string:

ShowMessage(IntToStr(Integer(cmbLanguage.Items.Objects[cmbLanguage.ItemIndex])));

Note that you are not really converting anything here, you're just pretending that your integer is a TObject so the compiler doesn't complain.

like image 140
Blorgbeard Avatar answered Nov 03 '22 01:11

Blorgbeard


If you know you will be using Delphi-7 for the rest of your life stick with the TObject(i) cast. Otherwise start using proper objects, this will save you headaches when upgrading to 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;

Storage:

SL := TStringList.Create(true); // 'OwnsObjects' for recent Delphi versions
IO := TIntObj.Create(123);  
SL.AddObjects(IO);

Retrieval:

IO := TIntObj(SL.Objects[4]);
ShowMessage('Integer value: '+ IntToStr(IO.I));

For Delphi-7

TIntObj := TStringList.Create;

and you have to free the objects yourself:

for i := 0 to Sl.Count-1 do 
begin
  IO := TIntObj(SL.Objects[i]);
  IO.Free;
end;
SL.Free;
like image 40
Jan Doggen Avatar answered Nov 02 '22 23:11

Jan Doggen