No announcement yet.

New to Objects - calling methods and functions

  • Filter
  • Time
  • Show
Clear All
new posts

  • New to Objects - calling methods and functions

    ' New to POO? Programming objects for the first time
    ' very simple example to show that
    ' a) you can call a class method from your main program and
    ' b) that it can call a "regular" function, and pass its own handle
    ' c) and the "regular" function can thus call the interface's methods
    '    without much fuss.
    ' d) Oh, and that just initialising the object calls the create Class Method
    ' e) ...and that discarding the object calls the Destroy method"
    ' Chris Holbrook Feb 2009
    #compile exe
    #dim all
    #debug display on  '<=========== a VERY GOOD THING when you first test the program
                       '             if an error occurs it will display details and prompt you
                       '             you will want to turn it off if you use the stepping deugger!
    function stdfun ( x as InterfaceX ) as long  '<======= The interface name used as the TYPE
                                                 '         of the parameter
         function = x.b       '<================= call the Interface's 'b' method
    end function
    class classX
        class method create
            ? "Hello, Creator!"
        end method
        class method Destroy
            ? "Goodbye, World! it was fun while it lasted!"
        end method
        interface InterfaceX
            inherit iunknown  '<======= you have to do this apparently
                              '         I'm not going to argue about it
                              '         ...just try leaving it out!
            method a
                local l as long
                ? "method 'a' here!"
                l = stdfun(me) '<============== call the regular function
                               '                passing the OBJECT handle - in this case,
                               '                the OBJECT is the InterfaceX interface
                ? str$(l)
            end method
            method b as long
                method = 1234
            end method
        end interface
    end class
    function pbmain () as long
        local xif as InterfaceX
        xif = class "classX" '<==== you have to do this too, to initialise the object
        xif.a                '<==== call the interface's 'a' method
        xif = nothing        '<==== tidying up by discarding the object
                             '      pretty irrelevant in THIS application
                             '      but could be essential - get into the habit!
    end function