Solved

Accessing/updating items in a simple TList of integers

Posted on 2002-04-25
6
313 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
[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 11

Expert Comment

by:shaneholmes
ID: 6969894
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
ID: 6969992
But I need Longint variables - integer might not be big enough.
0
 
LVL 12

Accepted Solution

by:
Lee_Nover earned 50 total points
ID: 6970051
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
Technology Partners: 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 1

Expert Comment

by:MBo
ID: 6970322
>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
ID: 6970325
Right - Integer and LongInt are the same

You can store longints in the list

Shane
0
 

Author Comment

by:aztec
ID: 6970383
worked great... thank you Mr. Nover  ;-)

Cheers
   Shawn
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Multiple image collision 13 111
Delphi: Connect to running MS Outlook 4 211
Broadcast a message using ICS 2 51
SQL Server Express automatically execute SQL or SP 8 70
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

710 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