Solved

Accessing/updating items in a simple TList of integers

Posted on 2002-04-25
6
314 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
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

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month4 days, 3 hours left to enroll

630 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