VBScript

Contributed by Chris Rathman

HTML File

<HTML>
<HEAD>
<TITLE>VBScript Shapes</TITLE>
<SCRIPT LANGUAGE="VBScript">
<!--

   Class Rectangle
      ' declare class attributes
      Dim x
      Dim y
      Dim width
      Dim height

      ' initialize the attributes (constructors not supported)
      Sub init(initx, inity, initwidth, initheight)
         x = initx
         y = inity
         width = initwidth
         height = initheight
      End Sub

      ' read accessors
      Function getX
         getX = x
      End Function
      Function getY
         getY = y
      End Function
      Function getWidth
         getWidth = width
      End Function
      Function getHeight
         getHeight = height
      End Function

      ' write accessors
      Sub setX(newx)
         x = newx
      End Sub
      Sub setY(newy)
         y = newy
      End Sub
      Sub setWidth(newwidth)
         width = newwidth
      End Sub
      Sub setHeight(newheight)
         height = newheight
      End Sub

      ' move the shape coordinates
      Sub moveTo(newx, newy)
         setX newx
         setY newy
      End Sub
      Sub rMoveTo(deltax, deltay)
         moveTo (x + deltax), (y + deltay)
      End Sub

      ' draw the rectangle
      Sub draw()
         Document.Write "Drawing a Rectangle at:(" & x & "," & y & _
            "), width " & width & ", height " & height & "<BR>"
      End Sub
   End Class

   Class Circle
      ' declare class attributes
      Dim x
      Dim y
      Dim radius

      ' initialize the attributes (constructors not supported)
      Sub init(initx, inity, initradius)
         x = initx
         y = inity
         radius = initradius
      End Sub

      ' read accessors
      Function getX
         getX = x
      End Function
      Function getY
         getY = y
      End Function
      Function getRadius
         getRadius = radius
      End Function

      ' write accessors
      Sub setX(newx)
         x = newx
      End Sub
      Sub setY(newy)
         y = newy
      End Sub
      Sub setRadius(newradius)
         radius = newradius
      End Sub

      ' move the shape coordinates
      Sub moveTo(newx, newy)
         setX newx
         setY newy
      End Sub
      Sub rMoveTo(deltax, deltay)
         moveTo (x + deltax), (y + deltay)
      End Sub

      ' draw the circle
      Sub draw()
         Document.Write "Drawing a Circle at:(" & x & "," & y & _
            "), radius " & radius & "<BR>"
      End Sub
   End Class

//-->
</SCRIPT>
</HEAD>

<BODY>
<SCRIPT LANGUAGE="VBScript">
<!--

   ' set up array with some shape instances
   Dim scribble(2)
   Set scribble(0) = New Rectangle
   Set scribble(1) = New Circle
   scribble(0).init 10, 20, 5, 6
   scribble(1).init 15, 25, 8

   ' iterate through the shapes
   For i = 0 To (UBound(scribble) - 1)
      scribble(i).draw
      scribble(i).rMoveTo 100, 100
      scribble(i).draw
   Next

   ' access a rectangle specific function
   Dim arectangle
   Set arectangle = New Rectangle
   arectangle.init 0, 0, 15, 15
   arectangle.setWidth 30
   arectangle.draw

//-->
</SCRIPT>
</BODY>
</HTML>

Output

Drawing a Rectangle at:(10,20), width 5, height 6
Drawing a Rectangle at:(110,120), width 5, height 6
Drawing a Circle at:(15,25), radius 8
Drawing a Circle at:(115,125), radius 8
Drawing a Rectangle at:(0,0), width 30, height 15

Chris Rathman / Chris.Rathman@tx.rr.com