VB6

  1. .cls files are used for VB "classes". These are COM classes. .bas files are used for global routines.
    The name of the .cls file is important: the progid for that class is made up: projectname.classname.XX
  2. Class initialize/terminate
          Private Sub Class_Initialize()
            'body
          End Sub
    
          Private Sub Class_Terminate()
            'body
          End Sub
         

    Class_initialize() corresponds to the FinalInitialize() in COM/ATL. It is called once, when the first reference to this component is created.

    Class_terminate() corresponds to the FinalTerminate() in COM/ATL. It is called once, when the reference count goes to 0.

  3. 'Options Explicit' causes the compiler to check for missing declarations etc.
  4. all data declarations are private by default. 'Public' causes the data to be visible in the component's interface
  5. Const xx = 9 creates a manifest constant. These can be public too.
  6. Variant types are VARIANT in C++. Variant's can hold any other data type.
  7. App.LogEvent "msg" causes a message to be written to the NT event log
  8. Set objXX = CreateObject("a.progid.goes.here") is late binding (run-time) of an object. The 'Set' is required. The IDE's intellisense does not work.
            dim XX as AReferenceGoesHere
            set XX = new AReferenceGoesHere
    The 'Set' is required. Intellisense works.
  9. Set XX = Nothing causes the object to be released. In COM/ATL the FinalRelease()
  10. If-then-else
          if  abc = 21 then
             abc = 22
          ElseIf abc = 22 Then
             abc = 23
          end if
          
  11. Loop structures
          dim i
          for i = 1 to 10
             'loop body
          next i
    
          Do While Not aValue
            ' loop body
          loop
    
          For Each Child In Children
             'loop body
          Next
          
  12. Misc. Flow of control
            Exit Sub
            Exit Function
    
          
  13. Exceptions Typical error handling:
          Sub xx
            On Error GoTo ErrHandler
    
            'sub body here
            Exit Sub
    
           ErrHandler:
              'error handler code here
              errNbr = Err.Number        'holds the error id
              errDescr = Err.Description 'a string explaining the error
         End Sub
          
    This causes any errors to set the Err global variable and continue on.
          On Error Resume Next
          
    This causes error handling to revert to the system default (i.e. put up a msg box.
          On Error GoTo 0
          
  14. String manipulations:
             UCase$("abc")    ' returns "ABC"
             Right$("ABC", 2) ' returns "BC"
             Left$("ABC", 1)  ' returns "A"
             Len$("ABC")      ' returns 3
             Trim("  ABC  ")  ' returns "ABC"
             Mid("ABC", 1, 1) ' returns "B"
          
  15. Conversions
             CInt("123")  ' returns an integer 123
             CStr(123)    ' returns a string "123"
             CSng("1.23") ' returns a single (C++ => float)
          
  16. Properties:
           Public Property Let CustomerID(RHS As String)
             mLocalVrbl = RHS
           End Property
    
           Public Property Get CustomerID() As Variant
             CustomerID = mLocalVrbl
           End Property
          
  17. Parameters:
          private sub XX(ByVal theIndex As String, ByRef theValue As String)
          
    All parameters are passed by reference. The ByVal is default and is not required.

    ByVal indicates that the reference is is a constant, e.g. it is an [in] parameter. The value contained in theIndex is not constant, it can be changed. However Strings are immutable and so the caller sees no changes. If theIndex was an object, it can be changed by XX.

    ByRef indicates that it is an [out] parameter. The reference can be changed. If theValue points to a new string, the caller sees the change.

  18. File System interaction
            Dim fs As Scripting.FileSystemObject
            Set fs = New Scripting.FileSystemObject
            If Not fs.FolderExists(sBuildPath) Then
               fs.CreateFolder (sBuildPath & "\")
            endif
    
            Dim aFlatFile As Scripting.TextStream
            Set aFlatFile = fs.CreateTextFile(sFileName, True)
            aFlatFile.WriteLine ("abc" & Chr(9) & "def")
            aFlatFile.Close
          
  19. The Message Box debugging methd
           MsgBox mAvariable