PDA

View Full Version : Need a little help with properties and classes.



gunnm
14-05-2003, 12:50 PM
Im toying around a little with delphi, and Im trying to make a board game.
It's a game of go 19x19.

I works fine and do all the stuff I want it to do etx. But I want to use a more OOP approach. So I make my a class called TGoban. (a go-board is called goban fyi)

It contains a "private" 2-dimensional array called matrix. And here is the problem. I want it to be a property, but everytime I try to change a value in my matrix it gives me stack overflow. I use a prpperty write function addStone(x,y, stone) which also is a member of TGoban of course, so I dont think its because access errors.

If I make TGoban.matrix public, then this works:
mygoban.matrix[6,3] := 3;
but this still doesnt:
addStone(x,y, stone); //stack overflow

(addStone(x,y,stone) basicly does this: matrix[x,y] := stone; )

It loses its whole purpouse with properties if I make it public.This just confuses me alot, so I thought you guys might have an idea of what Im doing wrong.

Im in school now, so I have no source atm. So if someone doesn't see any fundamental errors. I'll just bring some source in a day or two.

Thanks for any help.

Alimonster
20-05-2003, 08:02 AM
type
TTest = class
private
FBoard: array[0..7, 0..7] of Integer;
function GetValue(Row, Col: Integer): Integer;
procedure SetValue(Row, Col, NewValue: Integer);
public
property Board[Row, Col: Integer]: Integer read GetValue write SetValue; default;
end;

//...

function TTest.GetValue(Row, Col: Integer): Integer;
begin
Result := FBoard[row, col];
end;

procedure TTest.SetValue(Row, Col, NewValue: Integer);
begin
FBoard[row, col] := NewValue;
end;

Useless Hacker
21-05-2003, 07:59 AM
Probably the reason that you got a stack overflow was because you were reading/writing the property in the property read/write method, instead of the field, so it would keep on calling itself until it ran out of stack.

gunnm
26-05-2003, 11:33 AM
ohhh!! Now I get it! Thanks! :)