Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

TIntegerList ?!

Posted on 2000-04-25
6
Medium Priority
?
1,907 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 7

Accepted Solution

by:
Motaz earned 400 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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 tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

618 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