home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Visual Database / Visual BASIC 5.0 (Ent. Edition) / Vb5ent Extractor.EXE / VB / SAMPLES / PGUIDE / PROGWOB / PWOSBUS2.CLS < prev    next >
Encoding:
Visual Basic class definition  |  1996-11-26  |  3.2 KB  |  113 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "SmallBusiness2"
  6. Attribute VB_GlobalNameSpace = False
  7. Attribute VB_Creatable = False
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = False
  10. Option Explicit
  11. ' >> Best viewed in Full Module view. <<
  12. '
  13. ' Storage for debug ID number.
  14. Private mlngDebugID As Long
  15. Implements IDebug
  16.  
  17. ' Private Collection object is slightly
  18. '   more robust than a public Collection
  19. '   object, but it has problems.
  20. Private colEmployees As New Collection
  21.  
  22. ' Problem 1: The collection is private,
  23. '   so the Add method has to be part of
  24. '   the SmallBusiness2 class.  Since
  25. '   there may be many kinds of objects in
  26. '   a business, there may be many such
  27. '   methods, such as AddProduct.
  28. '
  29. Public Function AddEmployee(ByVal Name As String, ByVal Salary As Double) As Employee
  30.     Dim empNew As New Employee
  31.     Static intEmpNum As Integer
  32.     ' Using With shortens property references (.ID instead of empNew.ID)
  33.     ' and speeds up execution.
  34.     With empNew
  35.         ' Generate a unique ID for the new employee.
  36.         intEmpNum = intEmpNum + 1
  37.         .ID = "E" & Format$(intEmpNum, "00000")
  38.         .Name = Name
  39.         .Salary = Salary
  40.         ' Add the Employee object reference to the collection, using its
  41.         ' ID property as the key.
  42.         colEmployees.Add empNew, .ID
  43.     End With
  44.     ' The Add method should return a reference to
  45.     '   the new object.
  46.     Set AddEmployee = empNew
  47. End Function
  48.  
  49. ' Problem 1, continued:  There will also
  50. '   be Count functions for all the
  51. '   different business objects, and
  52. '   Delete methods, and so on.  Even
  53. '   with just Employee objects, the
  54. '   SmallBusiness2 class becomes very
  55. '   cluttered.
  56. '
  57. Public Function EmployeeCount() As Long
  58.     EmployeeCount = colEmployees.Count
  59. End Function
  60.  
  61. Public Sub DeleteEmployee(ByVal Index As Variant)
  62.     colEmployees.Remove Index
  63. End Sub
  64.  
  65. ' Problem 2: The Employees method can't
  66. '   be used with For Each.  You could make
  67. '   it work with For Each by returning a
  68. '   reference to the private collection,
  69. '   but then it's not a private collection
  70. '   any more, and you're right back in
  71. '   the House of Straw.
  72. '
  73. Public Function Employees(ByVal Index As Variant) As Employee
  74.     Set Employees = colEmployees.Item(Index)
  75. End Function
  76.  
  77. ' Problem 3: It's still possible for
  78. '   coding errors, this time within the
  79. '   vastly more complicated SmallBusiness
  80. '   object, to add invalid objects to the
  81. '   collection.
  82. Public Sub Trouble()
  83.     Dim X As New Employee
  84.     ' Because the Collection object accepts
  85.     '   a reference to any object, it's
  86.     '   still possible for an internal
  87.     '   coding error to add an uninitialized
  88.     '   Employee object.
  89.     colEmployees.Add X
  90. End Sub
  91.  
  92. Private Sub Class_Initialize()
  93.     mlngDebugID = DebugInit(Me)
  94. End Sub
  95.  
  96. Private Sub Class_Terminate()
  97.     DebugTerm Me
  98. End Sub
  99.  
  100. ' -------- IDebug Implementation --------
  101. '
  102. ' IDebug.DebugID gives you a way to tell
  103. ' ====== -------    objects apart.  It's
  104. '   required by the DebugInit, DebugTerm,
  105. '   and DebugShow debugging procedures
  106. '   declared in modFriend.
  107. '
  108. Private Property Get IDebug_DebugID() As Long
  109.     IDebug_DebugID = mlngDebugID
  110. End Property
  111.  
  112.  
  113.