Solved

TIntegerList ?!

Posted on 2000-04-25
6
1,688 Views
Last Modified: 2008-03-17
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
Comment
Question by:val84
6 Comments
 
LVL 7

Accepted Solution

by:
Motaz earned 100 total points
ID: 2746877
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
 
LVL 5

Expert Comment

by:TheNeil
ID: 2746899
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
 

Expert Comment

by:jonnyfive
ID: 2746920
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
LVL 2

Expert Comment

by:rene100
ID: 2747251
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
 

Author Comment

by:val84
ID: 2750259
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
 
LVL 7

Expert Comment

by:Motaz
ID: 2750536
Thanks.

Motaz
www.geocities.com/motaz1
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question