Solved

Accessing/updating items in a simple TList of integers

Posted on 2002-04-25
6
308 Views
Last Modified: 2010-04-04
Hi...
  I'm trying to create a simple TList of X # of integer items, initialize them all first to 0, then update certain ones in this list. It compiles clean, but I'm getting a run-time error though (EAccessViolation Error) ... here's my code:

Var
  cc_count : TList;
  tally : ^longint;

---------

cc_count:=TList.create;
{* the size of cc_count depends on another list *}
cc_count.Capacity:= otherlist.count;
for x:=0 to cc_count.count - 1 do
begin
  tally^:=0;
  cc_count.Add(tally);
end;

I'm getting the EAccessViolation error on the "tally^:=0;" line. Isn't that the way to set the value of a variable that's pointed to by a pointer?
  Anyway, my code goes in the following way to do the updating of items. Can you see if it's ok?

tally:=cc_count.items[x];
tally^:=tally^ + tempcount;
cc_count.items[x] := tally;


thanks!
   Shawn

0
Comment
Question by:aztec
6 Comments
 
LVL 11

Expert Comment

by:shaneholmes
Comment Utility
Try using an integer list, eliminating the need for pointers.

tintlist=class(tlist)
private
  procedure putint(index:integer;theint:integer);
  function getint(index:integer):integer;
public
  property Items[Index: Integer]: integer read Getint write Putint; default;
  function add(aitem:integer):integer;
  function Remove(Item: integer): Integer;
  function IndexOf(Item: integer): Integer;
  procedure Insert(Index: Integer; Item: integer);
end;
procedure tintlist.putint(index:integer;theint:integer);
begin
 inherited put(index,pointer(theint));
end;

function tintlist.getint(index:integer):integer;
begin
 result:=integer(inherited items[index]);
end;

function tintlist.add(aitem:integer):integer;
begin
 result:=inherited add(pointer(aitem));
end;

function tintlist.Remove(Item: integer): Integer;
begin
 result:=inherited remove(pointer(item));
end;

function tintlist.IndexOf(Item: integer): Integer;
begin
 result:=inherited indexof(pointer(item));
end;

procedure tintlist.Insert(Index: Integer; Item: integer);
begin
 inherited insert(index,pointer(item));
end;
0
 

Author Comment

by:aztec
Comment Utility
But I need Longint variables - integer might not be big enough.
0
 
LVL 12

Accepted Solution

by:
Lee_Nover earned 50 total points
Comment Utility
hehe aztec :)
Integer and LongInt are the same
and you don't need to create a new list type
simply typecast the pointer value to your integer value  and v/v like:

for x:=0 to cc_count.count - 1 do
begin
 cc_count.Add(Pointer(0));
end;

to write the value use:
List.Items[I]:=Pointer(SomeIntegerValue);

to read the value use:
SomeIntegerValue:=Integer(List.Items[I]);
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Expert Comment

by:MBo
Comment Utility
>cc_count.Capacity:= otherlist.count;
>for x:=0 to cc_count.count - 1 do
this is error - Capacity doesn't increase list.count!
you have to
for i:=0 to otherlist.count - 1 do
  cc_count.add(nil);
now cc_count contains zeros

or use Lee_Nover suggestion
0
 
LVL 11

Expert Comment

by:shaneholmes
Comment Utility
Right - Integer and LongInt are the same

You can store longints in the list

Shane
0
 

Author Comment

by:aztec
Comment Utility
worked great... thank you Mr. Nover  ;-)

Cheers
   Shawn
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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 I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 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

17 Experts available now in Live!

Get 1:1 Help Now