My problem with classlib in BBC SDL

Discussions related to the code libraries supplied with BB4W & BBCSDL
User avatar
zachnoland
Posts: 17
Joined: Sat 07 Dec 2024, 15:22
Location: somewhere in Southeast Asia

My problem with classlib in BBC SDL

Post by zachnoland »

I tried to follow the tutorial on bbcbasic wiki
https://www.bbcbasic.net/wiki/doku.php? ... rogramming

This is an example of my code, I tried to implement the formula for finding the area of a rectangle.

Code: Select all

   10 INSTALL @lib$+"classlib"
   15
   20 DIM ZRectangle{RLength, RWidth _Set_Area, _Get_Area}
   25
   30 DEF ZRectangle._Set_Area(RLength, RWidth)
   40 ZRectangle.RLength = RLength
   50 ZRectangle.RWidth = RWidth
   60 ENDPROC
   65
   70 DEF ZRectangle._Get_Area = ZRectangle.RLength * ZRectangle.RWidth
   75
   80 PROC_class(ZRectangle{})
   90 PROC_new(MYREC{}, ZRectangle{})
   95
  100 PROC(MYREC._Set_Area)(7.0, 4.0)
  110 PRINT "FORMULA FOR FINDING THE AREA OF A RECTANGLE"
  120 PRINT "RESULTS = "; FN(MYREC._Get_Area)
  125
  130 PROC_discard(MYREC{})
  135
  140 END
From the code above it produces an error. But I don't understand what's wrong with my code. I think I've written the code well.
the error so far shows at number 40.
Richard Russell
Posts: 366
Joined: Tue 18 Jun 2024, 09:32

Re: My problem with classlib in BBC SDL

Post by Richard Russell »

zachnoland wrote: Tue 13 May 2025, 00:59 From the code above it produces an error. But I don't understand what's wrong with my code.
The main problem with your code is that it does not adhere to this stated requirement at the Wiki:

"As with DEF PROC and DEF FN, single line definitions may be placed anywhere in the program, but multi line definitions must be placed where they won't be accidentally executed (normally at the end of the program, after the END statement)".

It is unfortunate that BBC BASIC imposes this restriction, because it means that the declaration of a Class and the definitions of its Methods cannot always be grouped together, but it is unavoidable.

So to make your code work you need to move the multi-line method definition to after the END statement, for example like this:

Code: Select all

   10 INSTALL @lib$+"classlib"
   20
   30 DIM ZRectangle{RLength, RWidth, _Set_Area, _Get_Area}
   40
   50 PROC_class(ZRectangle{})
   60 PROC_new(MYREC{}, ZRectangle{})
   70
   80 PROC(MYREC._Set_Area)(7.0, 4.0)
   90 PRINT "FORMULA FOR FINDING THE AREA OF A RECTANGLE"
  100 PRINT "RESULTS = "; FN(MYREC._Get_Area)
  110
  120 PROC_discard(MYREC{})
  130
  140 END
  150
  160 DEF ZRectangle._Get_Area = ZRectangle.RLength * ZRectangle.RWidth
  170
  180 DEF ZRectangle._Set_Area (RLength, RWidth)
  190 ZRectangle.RLength = RLength
  200 ZRectangle.RWidth = RWidth
  210 ENDPROC
User avatar
zachnoland
Posts: 17
Joined: Sat 07 Dec 2024, 15:22
Location: somewhere in Southeast Asia

Re: My problem with classlib in BBC SDL

Post by zachnoland »

Richard Russell wrote: Tue 13 May 2025, 06:16 So to make your code work you need to move the multi-line method definition to after the END statement, for example like this:

Code: Select all

   10 INSTALL @lib$+"classlib"
   20
   30 DIM ZRectangle{RLength, RWidth, _Set_Area, _Get_Area}
   40
   50 PROC_class(ZRectangle{})
   60 PROC_new(MYREC{}, ZRectangle{})
   70
   80 PROC(MYREC._Set_Area)(7.0, 4.0)
   90 PRINT "FORMULA FOR FINDING THE AREA OF A RECTANGLE"
  100 PRINT "RESULTS = "; FN(MYREC._Get_Area)
  110
  120 PROC_discard(MYREC{})
  130
  140 END
  150
  160 DEF ZRectangle._Get_Area = ZRectangle.RLength * ZRectangle.RWidth
  170
  180 DEF ZRectangle._Set_Area (RLength, RWidth)
  190 ZRectangle.RLength = RLength
  200 ZRectangle.RWidth = RWidth
  210 ENDPROC
Now this code looks like a procedure or function, it should be placed under END Lol.

But thanks for the explanation and correction :D
I now understand DEF PROC dan DEF FN single line.