Solved

TIntegerList ?!

Posted on 2000-04-25
6
1,602 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

757 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

18 Experts available now in Live!

Get 1:1 Help Now