Creating and Using Objects

Jump to Run BASIC Help File

Run BASIC has some object oriented facilities.   You can create your own object types in addition to the following built in ones:

Link
Textbox
Passwordbox
Textarea
Checkbox
Radiogroup
Graphic
Table
Xmlparser
File accessor
SQLite accessor
Null object

The way to create your own object type in Run BASIC is to create a program that you can use as an object.  Here is a really simple way to create an object that will load an image and display it with a title:

'titledImage.bas
'This object accepts an image filename and a title and
'displays them.

  global #image, title$, imageFile$

function setTitle(str$)
  title$ = str$
  call updateRendering
end function

function setImage(filename$)
  imageFile$ = filename$
  call updateRendering
end function

sub updateRendering
  cls
  if title$ <> "" and imageFile$ <> "" then
    loadgraphic #image, imageFile$
    howTall = #image height()
    #image color("white")
    xPosition = (#image width() - #image stringwidth(title$)) / 2
    #image place(xPosition, howTall - 14)
    #image "\";title$
    render #image
  end if
end sub

The above program defines an object that doesn't do anything by itself.  It needs to be run, and then the program that runs it can call the setTitle() and setImage() functions (or methods in object oriented terminology).  Here is just such an example.  

  run "titledImage.bas", #imageObject
  print "Type an image file: ";
  textbox #imagefield, "mandelbrot.jpg"
  print " Type a title: ";
  textbox #titlefield, "Fractal image"
  print
  link #showMe, "Show the image", [showIt]
  print
  render #imageObject
  wait

[showIt]
  #imageObject setImage(#imagefield contents$())
  #imageObject setTitle(#titlefield contents$())
  wait

RENDER and Objects

Notice that the titledImage.bas example uses RENDER to display its graphics, and the program that uses titledImage.bas also uses RENDER to display the titled image object.  If you remove either of these RENDER statements you will not see the graphic.

How does this work?  Each object that you create has it's own display buffer.  An object can use PRINT, HTML, CLS, and RENDER to create a presentation that can be displayed on the page.  Then a program that uses the object can use RENDER to insert that object's display buffer into the page.

Methods

Run BASIC object methods are defined by using the FUNCTION keyword.  The SUB keyword cannot be used to define methods.  All methods are public and can be called by any program that has access to the object.

Methods can return numbers:

  aNumber = #someObject getNumericValue()

or strings:

  aString$ = #someObject getString$()

or objects:

  #anObject = #someObject #getObject()

Error Handling

Each object that you create must manage its own error handling using the ON ERROR GOTO statement.  Any runtime error happening during a method call will cause the web application to stop executing with an error message.

Examples

Look at the examples named here to see code that shows how to create and use objects.

Project Uses as object
multicounter counter
programManager runWiki and multicounter
organizer groceries