Solved

TIntegerList ?!

Posted on 2000-04-25
6
1,632 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Activex get website that activex running on 3 85
How to fill array with TArray.Create? 14 79
Printing problem 2 81
Mobile Keyboard covers the display of the TMemo 3 78
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now