Solved

Accessing/updating items in a simple TList of integers

Posted on 2002-04-25
6
312 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
Independent Software Vendors: 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Create Database on Android via Delphi dbExpress 3 129
Delphi XE10 Round Image 2 186
HTML text in the body of an email (delphi code) 12 210
Delphi IDE crash without error message ... 7 114
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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

740 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