### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

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

• 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;
procedure writeval(AnIndex,Avalue:integer)
public
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.

#### jamie

• Hero Member
• Posts: 3774
##### 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;
7.         procedure writeval(AnIndex,Avalue:integer)
8.       public
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: 2994
• POKE 54296,15
##### 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.1.0 r64115 FPC 3.3.1 r40507 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: 4028
##### 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: 500
##### 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;
9.         procedure writeval(AnIndex,Avalue:integer)
10.       public
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,

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

#### MarkMLl

• Hero Member
• Posts: 1445
##### 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;beginresult:=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
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.