Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Accessing/updating items in a simple TList of integers

Posted on 2002-04-25
6
Medium Priority
?
316 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
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 200 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

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…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

782 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