Recent

Author Topic: Object is array of arrays. How to reference a single cell?  (Read 1078 times)

Grandad

  • Newbie
  • Posts: 2
Object is array of arrays. How to reference a single cell?
« on: September 23, 2020, 10:57:19 pm »
Hi all,

Well, this object-oriented thing is much harder than I anticipated. I have programmed in a variety of languages, but never OO before. Now I am trying to learn Object Pascal through Free-Pascal. The following question may be 'obvious' but I cannot see how to do it. An object is an array of arrays. So how do I reference one single cell?

Consider this cut down stuff:

//-----------------------------------start of snippet
type
  ToneD=array[0..9] of integer;
  TtwoD=Class(Tobject)
      private
        grid: array[0..9] of OneD;
        function readval(AnIndex:integer):integer;
        procedure writeval(AnIndex,Avalue:integer)
      public
        property cellvalue[AnIndex:integer]:integer read readval write writeval;
        function getmiddlecell:integer;
  end;

.
.
.
function getmiddlecell:integer;

begin
result:=grid[4][4] //does not work. what should it be?
end;
//----------------------------------------end of snippet



How do I write that "result=" line above? Any help appreciated.

Grandad

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Object is array of arrays. How to reference a single cell?
« Reply #1 on: September 23, 2020, 11:05:00 pm »
Code: Pascal  [Select][+][-]
  1. type
  2.   ToneD=array[0..9] of integer;
  3.   TtwoD=Class(Tobject)
  4.       private
  5.         grid: array[0..9] of OneD;
  6.         function readval(AnIndex:integer):integer;
  7.         procedure writeval(AnIndex,Avalue:integer)
  8.       public
  9.         property cellvalue[AnIndex:integer]:integer read readval write writeval;
  10.         function getmiddlecell:integer;
  11.   end;
  12.  
  13. .
  14. .
  15. .
  16. function getmiddlecell:integer; << Function TTwoD.Getmiddl...
  17.  
  18. begin
  19. result:=grid[4][4] //does not work. what should it be?
  20. end;
  21. //----------------------------------------end of snippet
  22.  
  23.  
you need to make it part of the class.
The only true wisdom is knowing you know nothing

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Object is array of arrays. How to reference a single cell?
« Reply #2 on: September 23, 2020, 11:08:56 pm »
And one typo:
Code: Pascal  [Select][+][-]
  1. grid: array[0..9] of OneD;  // must be ToneD;
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Object is array of arrays. How to reference a single cell?
« Reply #3 on: September 23, 2020, 11:17:34 pm »
Is it just me? The naming of the types is a bit confusing.
If TOneD is an array[0..9] of integer, I would expect TTwoD to be an array[0..9,0..9] of integer?

Bart

egsuh

  • Hero Member
  • *****
  • Posts: 1273
Re: Object is array of arrays. How to reference a single cell?
« Reply #4 on: September 24, 2020, 09:36:44 am »
Quote
If TOneD is an array[0..9] of integer, I would expect TTwoD to be an array[0..9,0..9] of integer?

Respecting the above comment, I would write as:

Code: Pascal  [Select][+][-]
  1. type
  2.   ToneD=array[0..9] of integer;
  3.   TtwoD=array [0..9] of TOneD;
  4.  
  5.   TtwoDobj=Class
  6.       private
  7.         grid: TTwoD;
  8.         function readval(AnIndex:integer):integer;
  9.         procedure writeval(AnIndex,Avalue:integer)
  10.       public
  11.         property cellvalue[AnIndex:integer]:integer read readval write writeval;
  12.         function getmiddlecell:integer;
  13.   end;
  14.  
  15. .
  16. .
  17. function TTwoDObj.getmiddlecell:integer;
  18. begin
  19.      result:=grid[4][4];
  20. end;


And other function and property will need two parameters. For example,

   function readval(FirstIndex, secondindex: integer): integer;

should be defined like this, regardless of whether you define TTwoD as class or as array as I did.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Object is array of arrays. How to reference a single cell?
« Reply #5 on: September 24, 2020, 09:48:07 am »
Welcome aboard old chap, and hope you're enjoying the experience.

Code: [Select]
type
  ToneD=array[0..9] of integer;
  TtwoD=Class(Tobject)
      private
        grid: array[0..9] of OneD;
...

function getmiddlecell:integer;

begin
result:=grid[4][4] //does not work. what should it be?
end;

getMiddleCell() is an ordinary function, not a method associated with the class. As a result it has limited access to the grid 2D array since it's marked private. There are three possible solutions: make the function part of the class (assuming it's in the same unit, otherwise you'll be messing with helpers), move grid into the public part, or access it via a property.

The syntax for 2D properties temporarily eludes me. Also I don't know whether it can be generalised for more than two indices.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Grandad

  • Newbie
  • Posts: 2
Re: Object is array of arrays. How to reference a single cell?
« Reply #6 on: September 24, 2020, 07:36:58 pm »
Thank you to all who contributed comments. As a newbie, there is much for me to consider. Let us hope I can get my head around this.

Grandad

 

TinyPortal © 2005-2018