Array of Points not working correctly...

Posted by dbeattie on 28-Jul-2009 10:13

I'm trying to figure out why the resulting array of type common.class.point is always showing the first point loaded for every extent when "reviewing" the points inthe last loop. The loading loop shows the correct points, but the second doesn't. Should I not be doing things like this?

Thanks,

Don.

test code

DEFINE VARIABLE cPoints AS CHARACTER   NO-UNDO
   INITIAL "5.96575367106554,-75.1025390625 5.87833210967433,-71.54296875 3.29408222831282,-74.970703125 4.91583280131316,-76.2890625 5.96575367106554,-75.1025390625":u.
DEFINE VARIABLE points AS common.class.Point NO-UNDO EXTENT.
DEFINE VARIABLE iLoop AS INTEGER     NO-UNDO.

EXTENT(points) =  NUM-ENTRIES(cPoints, " ":u).
ASSIGN points  = NEW common.class.Point().

DO iLoop = 1 TO NUM-ENTRIES(cPoints, " ":u):
   ASSIGN points[iLoop]:Y = DECIMAL(ENTRY(1,ENTRY(iLoop, cPoints, " ":u)))
          points[iLoop]:X = DECIMAL(ENTRY(2,ENTRY(iLoop, cPoints, " ":u)))
        NO-ERROR.
   MESSAGE "Loading Point" iLoop ":" points[iLoop]:X points[iLoop]:Y  VIEW-AS ALERT-BOX.
END.

DO iLoop = 1 TO  NUM-ENTRIES(cPoints, " ":u):
    MESSAGE "Review Point" iLoop ":" points[iLoop]:X points[iLoop]:Y  VIEW-AS ALERT-BOX.
END.

common.class.Point.cls

CLASS common.class.Point:
  DEFINE PUBLIC VARIABLE X AS DECIMAL NO-UNDO INITIAL 0.
  DEFINE PUBLIC VARIABLE Y AS DECIMAL NO-UNDO INITIAL 0.
END CLASS.

All Replies

Posted by dbeattie on 28-Jul-2009 10:18

Actually it appears to be retaining the last point stored for each extent.

Posted by Peter Judge on 28-Jul-2009 10:20

DEFINE VARIABLE points AS common.class.Point NO-UNDO EXTENT.

DEFINE VARIABLE iLoop AS INTEGER     NO-UNDO.

EXTENT(points) =  NUM-ENTRIES(cPoints, " ":u).
ASSIGN points  = NEW common.class.Point().

The last line sets each extent to the same instance of Point (ie there's only one instance of Point() ). So you're overwriting the X and Y values with each iteration.

-- peter

Posted by dbeattie on 28-Jul-2009 10:24

Oops, thanks. Working great now.

Posted by Thomas Mercer-Hursh on 28-Jul-2009 11:39

Also, please don't declare public data members.  Use properties ... simple interface, but you retain the control over what happens inside the class.

This thread is closed