/ Forums / Advansys Formativ / Formativ Creator / Organizing Code with Classes

  • Creator
    Topic
  • #3611
    ctaleck
    Participant

    I have found that it is not possible to directly call subs in a class on form events. E.g.

    OnClick = myClass.myButtonClick

    As my coding becomes larger with multiple forms, I am looking for ways to organize it with classes. But this limitation does not let me associate all methods with a single form/class making the Procedures list not as organized as I would like.

    What other methods are people using to help them organize their code?

  • Author
    Replies
  • #5632
    Support 3
    Participant

    As you probably know, in VBS, you can create Class to defines an object, its properties and methods to control the object’s behavior. Please see the Visual Basic Script Guide for more information. You can access the guide from the Formativ Help menu.

    The example code below create a class when then Applet start. The class object is destroyed at the end so you can use it in throughout the Applet (i.e. Form events, etc).

      
    dim gMyClass
    
    '-------------------------------------------------------------------------------
    ' Main line processing
    '-------------------------------------------------------------------------------
    
    Sub Main(Client, GWEvent)
    
      set gMyClass = new CMyClass
    
    
      msgbox "Inside Main sub: " & gMyClass.DateTime
      gMyClass.UpdateDateTime()
      msgbox "Inside Main sub: " & gMyClass.DateTime
    
    
      MyDialog.showmodal
    
      set gMyClass = nothing
      
    End Sub
    
    
    '================================================================================
    ' Description about my class
    '================================================================================
    class CMyClass
    
      private iDateTime
    
    
      ' Constructor
      private sub class_initialize
        iDateTime =  now
      end sub
    
    
      ' Destructor
      private sub class_terminate
    
      end sub
    
    
      ' Update date time
      public sub UpdateDateTime()
        iDateTime =  now
      end sub
    
    
      public property get DateTime()
        DateTime = iDateTime
      end property
    
    
      public property let DateTime(aVal)
        iDateTime = aVal
      end property
    
    end class
    
    
    '================================================================================
    ' On button click in the form
    '================================================================================
    Sub MyDialogButton1Click(Sender)
      msgbox "on button click: " & gMyClass.DateTime
      gMyClass.UpdateDateTime()
      msgbox "on button click: " & gMyClass.DateTime
    End Sub
    

    Hope I haven’t misunderstood your enquiry.

    Regards,
    Advansys Support

    #5633
    Support 1
    Participant

    Unfortunately the mechanism used in Formativ to handle form/control events only supports standalone procedures. A VBScript class method cannot be an event handler.

    For the moment the only way I can see to organize your procedures is by form name. That is, when an event handler procedure is created, the designer automatically gives it a name based on its owner form, eg. where the form name is “MyDialog”:

    Sub MyDialogButton1Click(Sender)

    Then all handlers belonging to a given form will appear together, in alphabetical order, in the Procedures group of the Code Explorer (treeview of globals, classes, procedures).

    Note that it is possible to assign a handler to multiple events, even in different forms. This is useful when you want to perform the same action in different contexts. In this case I would try to keep things simple by giving the handler a general name. It may be appropriate for the handler to delegate to an object method (as with gMyClass.UpdateDateTime() in the above sample from Support 3), particularly if the object method is complex or depends on related methods/fields. In this way you can encapsulate much of the complexity inside a class. Obviously, a global reference to the object will be required, like the variable gMyClass.

    I hope this helps.

    Regards,
    Advansys Support

    #5630
    ctaleck
    Participant

    Thank you for all your suggestions. I am already utilizing the methods described, but I wanted to push it further to help organize my ever-increasing code base more.

    #5631
    Support 1
    Participant

    You are welcome.

    In some circumstances it may be preferable to develop part of a GroupWise solution as a COM/ActiveX object. The object can then be used by Formativ/VBScript in whatever GroupWise contexts the solution is required.

    I expect you are aware that developing a COM/ActiveX object requires a COM-compatible language, eg. C++, Delphi, VB or any of the languages that support Microsoft .NET. This post describes the issues to be aware of when developing a COM/ActiveX object using .NET.

    Regards,
    Advansys Support

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.