[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1946
  • Last Modified:

TIntegerList ?!

Hi everybody.
As it is known there is a very useful object named "TStringList" which is a list of strings. I am curious if Delphi offers the posibility for EASY defining lists of other types: integer, single etc.
Regards, Alex
0
val84
Asked:
val84
1 Solution
 
MotazCommented:
Unfortunatly, there is no equivelant for other types for TStringList, but you can:

1. Convert integer to string and store it in TStringList
2. Inherit TList to support any type, even user defiened.
3. Use Dyanmic array, when you want to add new item you can:

a: array of integer;
begin
  SetLength(a, 1);
  a[0]:= 123;
  SetLength(a, high(a) + 2) // to add new item

  SetLegnth(a, 0); // erase

Motaz
0
 
TheNeilCommented:
You could take a look at the declaration for TStringList and create a new class called TIntegerList. It would involve a fair amount or work though as you're writing a new class. A better way might just be to cheat and use a TStringList, converting your integers to/from strings

The Neil =:)
0
 
jonnyfiveCommented:
There is a easy way to store Integers. Use a TList and type cast the integer to a pointer and the other way around if you want to retrieve it.

MyTList.Add(Pointer(12345));

MyInteger := Integer(MyTList[0]);

Hope this helps, Jonny...
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
rene100Commented:
Hi

I've once created such a class:

unit IntList;

interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Buttons, ToolWin, ComCtrls, Menus, StdCtrls, ExtCtrls,Clipbrd;

type
  TIntList=class(TList)
private
  function GetItem(Index: integer): integer;
published
  property Items[Index: integer]: Integer read GetItem;
  function Add(Item: integer): Integer;
  function GetASString(Index: integer): string;
  function MaxValue: integer;
  function MinValue: integer;
  function MaxValueIndex: Integer;
  function MinValueIndex: Integer;
  function IndexOf(Item: Integer): integer;
  procedure Insert(Index,Item: Integer);
end;

implementation

function TIntList.GetItem(Index: Integer): Integer;
begin
Result:=Integer(TList(Self).Items[Index]);
end;

function TIntList.IndexOf(Item: Integer): integer;
begin
Result:=inherited IndexOf(Pointer(Item));
end;

function TIntList.Add(Item: integer): integer;
Var
PtrI: Pointer;
begin
PtrI := Pointer(Item);
Result := inherited Add(PtrI);
end;

function TIntList.GetAsString(Index: integer): string;
begin
Result:=IntToStr(Items[Index]);
end;

function TIntList.MaxValue: Integer;
Var
Index, TempMax: integer;
begin
TempMax:=-2147483647;
For Index:=Count-1 downto 0 do
    begin
    if Items[Index]>TempMax then
       TempMax:=Items[Index];
    end;
Result:=TempMax;
end;

function TIntList.MinValue: Integer;
Var
Index, TempMin: integer;
begin
TempMin:=2147483647;
For Index:=Count-1 Downto 0 do
    begin
    if Items[Index]<TempMin then
       TempMin:=Items[Index];
    end;
Result:=TempMin;
end;

function TIntList.MaxValueIndex: Integer;
Var
Index, TempMax: integer;
begin
TempMax:=Count-1;
For Index:=Count-1 downto 0 do
    begin
    if Items[Index]>TempMax then
       TempMax:=Index;
    end;
Result:=TempMax;
end;

function TIntList.MinValueIndex: Integer;
Var
Index, TempMin: integer;
begin
TempMin:=Count-1;
For Index:=Count-1 Downto 0 do
    begin
    if Items[Index]<TempMin then
       TempMin:=Index;
    end;
Result:=TempMin;
end;

procedure TIntList.Insert(Index,Item: Integer);
begin
inherited Insert(Index,Pointer(Item));
end;
end.

regards
rene
0
 
val84Author Commented:
Thanks all of you, and especially to Rene for his detailed answer. I accepted Motaz's answer not only because it was the first, but also for his ideea of dynamic arrays.
Regards, Val.
0
 
MotazCommented:
Thanks.

Motaz
www.geocities.com/motaz1
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now