Procedures Versus Methods


Object-oriented programming turns traditional functional programming on its ear. In functional programming, you call a procedure indicating what you want to do and then pass arguments specifying what to do it to, and how to do it:

DoThis ToThat, WithThese

In object-oriented programming, you indicate what you want to work on and then specify methods to indicate what you want to do:

ToThis.DoThat WithThese

In traditional functional programming, you can pass two kinds of arguments: one indicates what you want to work on, and the other provides additional information about how to do the work. In object-oriented programming, you sometimes don’t need to indicate what you want to work on, because a default object is understood. In Visual Basic terms, the statement

DoThis WithThese

might mean

Me.DoThis WithThese

At other times, you don’t need to specify how to work on something because you have only one possibility:

DoThis    ‘ WithDefaultThese

Internally, the processor doesn’t know anything about object-oriented programming; assembly language is always functional. Object-oriented languages fake object-oriented programming internally by passing a hidden argument. When you write the code

ToThis.DoThat WithThese

in Visual Basic (or any object-oriented language), what the processor really sees, in simplified terms, is this:

DoThat ToThis, WithThese

To attach the DoThat method to the ToThis object in Visual Basic, you create a class for CThis objects, of which ToThis will be an instance. Within the class, you create a public DoThat sub and give it a WithThese parameter. When you create a new ToThis instance of the CThis class, you can call DoThat using object-oriented syntax:

Dim ToThis As New CThis
ToThis.DoThat WithThese

Of course, you can still use the functional style on objects. You must be able to do this so that one object can work on another. Only one object can be the base object, so if there are two objects involved, one has to be passed as a method parameter.


Creating a functional version of a method is easy. Assume you have the following object method:

Private iCount As Integer
§
Sub Square()
iCount = iCount * iCount
End Sub

It’s easy to create a functional version that uses it:

Sub SquareThing(ByVal thing As CThing)
thing.Square
End Sub

This procedure can be either a Sub in a standard module, or a method in another class module.