Lazarus

Free Pascal => Beginners => Topic started by: Grandad on September 23, 2020, 10:57:19 pm

Title: Object is array of arrays. How to reference a single cell?
Post by: Grandad 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
Title: Re: Object is array of arrays. How to reference a single cell?
Post by: jamie 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.
Title: Re: Object is array of arrays. How to reference a single cell?
Post by: Blaazen on September 23, 2020, 11:08:56 pm
And one typo:
Code: Pascal  [Select][+][-]
  1. grid: array[0..9] of OneD;  // must be ToneD;
Title: Re: Object is array of arrays. How to reference a single cell?
Post by: Bart 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
Title: Re: Object is array of arrays. How to reference a single cell?
Post by: egsuh 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.
Title: Re: Object is array of arrays. How to reference a single cell?
Post by: MarkMLl 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
Title: Re: Object is array of arrays. How to reference a single cell?
Post by: Grandad 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