Recent

Author Topic: Assigned(mycomp[n]) Strange error.  (Read 218 times)

McClane

  • New Member
  • *
  • Posts: 22
Assigned(mycomp[n]) Strange error.
« on: August 23, 2019, 11:26:05 pm »
Hi, I'm having a strange error with Assigned();
I made a component like a TMemo and I have an array of them.
I've made a function wich I give the component name and it returns the array number. If the component doesn't exists, I get some numbers like 31255, -5897 and -7665.
So I give n a valid number, here we go:
n:= 0;
x:= function('irreal name');
if assigned(mycomp
  • ) then n:= x;


when mycomp[n] is used I get the error.
I hope I have made myself clear.
Have a nice day.

jamie

  • Hero Member
  • *****
  • Posts: 1924
Re: Assigned(mycomp[n]) Strange error.
« Reply #1 on: August 23, 2019, 11:45:51 pm »
First of all, you need to clean up  your arrays to all NILS or 0's to start with.

Also you should use a Function that accepts your index and then have that function range test the index to ensure it's within a valid range. If that passes the test, then you can proceed to use that INDEX or return a default, false value..

 For example:

Var
  A:Array[0...100] of MyComponents;


 Index 0 would be no component found, but index 1..n would be a valid component.

 This way if you index the array,  you don't fault it but you can simply return a null component.

lucamar

  • Hero Member
  • *****
  • Posts: 1991
Re: Assigned(mycomp[n]) Strange error.
« Reply #2 on: August 24, 2019, 12:07:27 am »
For those kind of things I use this function:

Code: Pascal  [Select]
  1. function InRange(const Index, Lowest, Highest: Integer;
  2.   Inclusive: Boolean): Boolean;
  3. begin
  4.   if Inclusive then
  5.     Result := (Index >= Lowest) and (Index <= Highest)
  6.   else
  7.     Result := (Index > Lowest) and (Index < Highest);
  8. end;

For an array of controls or components use it as:

Code: Pascal  [Select]
  1. function GetItem(const Index: Integer): TSomeComponent;
  2. begin
  3.   if InRange(Index, Low(ArrayOfItems), High(ArrayOfItems)
  4.   and Assigned(ArrayOfItems[Index]) then
  5.     Result := ArrayOfItems[Index]
  6.   else
  7.     Result := Nil;
  8. end;

But yes, to use Assigned effectively you have first to make sure the unassigned items in the array are set to Nil
« Last Edit: August 24, 2019, 12:09:57 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

McClane

  • New Member
  • *
  • Posts: 22
Re: Assigned(mycomp[n]) Strange error.
« Reply #3 on: August 24, 2019, 07:14:13 am »
I understand, thank you both of you.
Well I'm sure the items are nil because the error occurs at the beginning of the program.

lucamar

  • Hero Member
  • *****
  • Posts: 1991
Re: Assigned(mycomp[n]) Strange error.
« Reply #4 on: August 24, 2019, 01:12:07 pm »
It's difficult to help with more than generalities. We'd need to see more code to be able to understand what may be happenning, like how and when the components are created, how the array is declared/initialized/populated, etc.

If you could createa small project demonstrating the issue, even if using the standard memo, that would help enormously.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.