How to save Integer in the TObject property and then show this value to the user?

Of course, this piece of code will not compile. First I need to specify the TObject value for 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 you can use String instead of Integer in the first place?

+5
source share
4 answers
cmbLanguage.Items.AddObject(Format('Item %d', [i]), TObject(i));

Here you add an element with an “object”, which is actually an integer ( i) cast from TObject.

Since you are actually storing an int in an object field, you can simply return it back to Integer and then convert it to a string:

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

, , , TObject, .

+9

, Delphi-7 , TObject (i). , 64 .

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.

:

var
  IO: TIntObj;
  SL: TStringList;

:

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

:

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

Delphi-7

TIntObj := TStringList.Create;

:

for i := 0 to Sl.Count-1 do 
begin
  IO := TIntObj(SL.Objects[i]);
  IO.Free;
end;
SL.Free;
+2

delphi xe , , @Jerry.

:

unit CoreClasses;

interface

type
  IPrimitiveBox<T> = interface

    procedure setValue(value : T);
    function getValue(): T;

  end;

  TPrimitiveBox<T> = class(TInterfacedObject, IPrimitiveBox<T>)

    private
      value : T;

    public
      constructor create(value : T);

      // IPrimtiveBox<T>
      procedure setValue(value : T);
      function getValue(): T;

  end;

implementation

{ TPrimitiveBox<T> }

constructor TPrimitiveBox<T>.create(value: T);
begin
  self.value := value;
end;

function TPrimitiveBox<T>.getValue: T;
begin
  Result := value;
end;

procedure TPrimitiveBox<T>.setValue(value: T);
begin
  self.value := value;
end;

( @Jerry)

var
  io: IPrimitive<Integer>;

sl := TStringList.create(true);
io := TPrimitive<Integer>.create(123);
sl.addObjects(io)


io := IPrimitive<Integer>(sl.objects[4]);
ShowMessage('Integer value: '+ IntToStr(io.getValue()));
+2
source

You cannot just convert an object to a string. This is what you will need to do yourself, using the method of your choice, depending on the reasons behind it. For example, you can concatenate an XML string representing the data in your object. However, Delphi has absolutely no way to concatenate this data for you.


As others pointed out, you are actually trying to use TObjecthow Integer. This means that if you saved an integer in a field TObject, you need to discard it, for exampleInteger(MyIntObject)

+1
source

All Articles