VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

Systems Analysis - System analysis and Design tutorial for Software Engineering

You are here: Visual Basic > Advanced VB6 tutorial > Chapter 12

Implementing Custom Methods in Class Modules

When you create methods in a class, you can call them in the rest of your code just as you would call methods for any standard VB class.

To provide methods for your class, just create Public procedures in the class module (both Sub and Function procedures will work). Code from other parts of the application or in ActiveX controllers can then call these procedures directly as methods of the object Class.

The example in Listing 12.1 defines a Public Sub procedure SearchPath in the class module FileFind. Code residing elsewhere in the application can call the SearchPath method of the object Class. The calling code passes to the method the name of the directory to search and a file specification to search for.


[In the Class module]
Public Sub SearchPath(strDirToSearch As String, _
            strFileSpec As String)

      'Add a backslash to the directory
      'name if one's not there already
      If Right(strDirToSearch, 1) <> "\" Then
            strDirToSearch = strDirToSearch & "\"
      End If

      'Get the name of the first file in the
      'directory fitting the specified pattern
      Dim strName As String
      strName = Dir$(strDirToSearch & strFileSpec)

      'Loop through all file names in the directory
      'fitting the specified pattern
      Do Until strName = ""
          'Add this file to our list
          frmFiles.lstFiles.AddItem strName
          'get the next file name in the list
          strName = Dir$
End Sub

[In the General Declarations of a Form]
Private FF As FileFind
[In the calling routine]
Private Sub cmdFindFiles_Click()
      Set FF = New FileFind
      FF.SearchPath _
            dirFind.Path, _
End Sub


<< Previous | Contents | Next >>

Home | About Us | Privacy Policy | Contact Us

Copyright © | All Rights Reserved