[Tutor] OOP programming using VB

Alan Gauld alan.gauld at blueyonder.co.uk
Sat Jan 24 13:07:26 EST 2004


> I have never done OO programming using VB . Anyway I have used OOP
> concepts with C++. I have no clear idea of how to use OOP concepts
> with VB forms and all,

Its pretty similar to C++. A form is an object in VB and it has
methods etc which you can call. When you add components you
define an action or event handler which is effectively a
method of the form object.

You can also define your own classes using a blank code page
like this rather trivial example copied from the paper version
of my book...

Class Message
    Private theText
    Public Property Let Text(S)
        theText = S
    End Property
    Pubic Sub Print()
        If theText = "" Then
            WSCript.echo "No Message"
        Else
            For n = 1 to 3
                WSCript.echo theText
            Next
        End If
    End Sub
End Class

Dim Msg
set Msg = new Message
Msg.Text = "Hello world"
Msg.Print()

The equivalent code in Python (Since this is a Python mailing
list) is as follows:

class Message:
   def __init__(self, text = ""):
      self.theText = text
   def Print(self):
       if self.theText == "":
            print "No Message"
       else:
            for n in range(3):
                print self.theText

msg = Message("Hello world")
msg.Print()

I'll let you figure out the equivalent C++ (Or you can buy the book!
:-)

NOTE the above example is actually in VBScript. There might be
some minor syntax diffrences in VB proper but its very similar.

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list