home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 4 Power Pack / Visual_Basic4_Power_Pack.bin / vb4files / vbof / demoaddr.cls next >
Encoding:
Text File  |  1996-11-20  |  15.2 KB  |  566 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "Address"
  6. Attribute VB_Creatable = False
  7. Attribute VB_Exposed = True
  8. Option Explicit
  9.  
  10. ' the following pertain to being supported by
  11. '   VBOFCollection, VBOFObjectManager and
  12. '   VBOFEventManager
  13. Public ObjectID As Long
  14. Public ObjectChanged As Long
  15. Public ObjectAdded As Long
  16. Public ObjectDeleted As Long
  17. Public ObjectParentCount As Long
  18. Public ObjectManager As VBOFObjectManager
  19.  
  20. ' the following code pertains to the business
  21. '   of the Address object
  22. Private pvtLine1 As String
  23. Private pvtLine2 As String
  24. Private pvtLine3 As String
  25. Private pvtCity As String
  26. Private pvtStateCode As String
  27. Private pvtState As State
  28. Private pvtStateObjectID As Long
  29. Private pvtZipCode As Long
  30. Private pvtZipSupplement As Integer
  31. Private pvtZipExtension As Integer
  32. Private pvtForeignZipCode As String
  33. Private pvtStatus As String
  34. Private pvtUsage As String
  35. Public StateCodes As VBOFCollection
  36.  
  37. Public Function ObjectDBGridUnboundAddData(Optional DBGrid As Variant, Optional RowBuf As Variant, Optional NewRowBookmark As Variant) As Boolean
  38. ' Populate the object variables with the values
  39. '   provided by the user in the new row of the
  40. '   DBGrid
  41. '   (in support of VBOFCollection)
  42. '
  43. ' Parameter Description:
  44. '   DBGrid:= the DBGrid which is being
  45. '       populated
  46. '   RowBuf:= the current DBGrid RowBuf object
  47. '   NewRowBookmark:= the row number being processed
  48.  
  49.     Dim I As Long
  50.     
  51.     For I = 0 To RowBuf.ColumnCount - 1
  52.         If Not IsNull(RowBuf.Value(0, I)) Then
  53.             Select Case RowBuf.ColumnName(I)
  54.                 Case "Line1"
  55.                     Line1 = RowBuf.Value(0, I) & ""
  56.                 Case "Line2"
  57.                     Line2 = RowBuf.Value(0, I) & ""
  58.                 Case "Line3"
  59.                     Line3 = RowBuf.Value(0, I) & ""
  60.                 Case "City"
  61.                     City = RowBuf.Value(0, I) & ""
  62.                 Case "StateCode"
  63.                     StateCode = RowBuf.Value(0, I) & ""
  64.                 Case "ZipCode"
  65.                     ZipCode = CLng("0" & RowBuf.Value(0, I))
  66.                 Case "ZipSupplement"
  67.                     ZipSupplement = CLng("0" & RowBuf.Value(0, I))
  68.                 Case "ZipExtension"
  69.                     ZipExtension = CLng("0" & RowBuf.Value(0, I))
  70.                 Case "Status"
  71.                     Status = RowBuf.Value(0, I) & ""
  72.                 Case "Usage"
  73.                     Usage = RowBuf.Value(0, I) & ""
  74.     
  75. ' Note:  Do not initialize the ObjectID.
  76.         
  77.             End Select
  78.         End If
  79.     Next I
  80.     
  81. ' return "OK" status
  82.     ObjectDBGridUnboundAddData = True
  83. End Function
  84.  
  85. Public Function ObjectDBGridUnboundReadData(Optional DBGrid As Variant, Optional RowBuf As Variant, Optional RowNumber As Variant) As Boolean
  86. ' Populate the DBGrid RowBuf with values from
  87. '   variables within this object
  88. '   (in support of VBOFCollection)
  89. '
  90. ' Parameter Description:
  91. '   DBGrid:= the DBGrid which is being
  92. '       populated
  93. '   RowBuf:= the current DBGrid RowBuf object
  94. '   RowNumber:= the row number being processed
  95.  
  96.     Dim I As Long
  97.     
  98.     For I = 0 To RowBuf.ColumnCount - 1
  99.         Select Case RowBuf.ColumnName(I)
  100.             Case "Line1"
  101.                 RowBuf.Value(RowNumber, I) = Line1
  102.             Case "Line2"
  103.                 RowBuf.Value(RowNumber, I) = Line2
  104.             Case "Line3"
  105.                 RowBuf.Value(RowNumber, I) = Line3
  106.             Case "City"
  107.                 RowBuf.Value(RowNumber, I) = City
  108.             Case "StateCode"
  109.                 RowBuf.Value(RowNumber, I) = StateCode
  110.             Case "ZipCode"
  111.                 RowBuf.Value(RowNumber, I) = ZipCode
  112.             Case "ZipSupplement"
  113.                 RowBuf.Value(RowNumber, I) = ZipSupplement
  114.             Case "ZipExtension"
  115.                 RowBuf.Value(RowNumber, I) = ZipExtension
  116.             Case "Status"
  117.                 RowBuf.Value(RowNumber, I) = Status
  118.             Case "Usage"
  119.                 RowBuf.Value(RowNumber, I) = Usage
  120.             Case "FormattedZip"
  121.                 RowBuf.Value(RowNumber, I) = FormattedZip
  122.             Case "ForeignZipCode"
  123.                 RowBuf.Value(RowNumber, I) = ForeignZipCode
  124.             Case "ObjectID"
  125.                 RowBuf.Value(RowNumber, I) = ObjectID
  126.         End Select
  127.     Next I
  128. End Function
  129. Public Function ObjectDataSource() As String
  130. ' Return the Data Source with which this Class is associated
  131. '   (in support of VBOFCollection)
  132.     
  133.     ObjectDataSource = "Addresses"
  134. End Function
  135.  
  136. Public Function FormattedAddress() As String
  137. ' Return a displayable, fully formatted
  138. '   version of Me
  139.  
  140.     Dim ReturnString As String
  141.     
  142.     If Line1 > "" Then
  143.         ReturnString = _
  144.             pvtContatenateWithCRLF( _
  145.                 String1:=ReturnString, _
  146.                 String2:=Line1)
  147.     End If
  148.     
  149.     If Line2 > "" Then
  150.         ReturnString = _
  151.             pvtContatenateWithCRLF( _
  152.                 String1:=ReturnString, _
  153.                 String2:=Line2)
  154.     End If
  155.     
  156.     If Line3 > "" Then
  157.         ReturnString = _
  158.             pvtContatenateWithCRLF( _
  159.                 String1:=ReturnString, _
  160.                 String2:=Line3)
  161.     End If
  162.     
  163.     If City > "" Then
  164.         ReturnString = _
  165.             pvtContatenateWithCRLF( _
  166.                 String1:=ReturnString, _
  167.                 String2:=City) _
  168.                 & ", "
  169.     End If
  170.     
  171. ' the conventional (i.e. GUI-centric) VB programming
  172. '   techniques must use StateCode.  The Most
  173. '   Totally Coolest way (the OO way) is to imbed the
  174. '   entire State object within the Address.  Yields
  175. '   not only the StateCode, but anything else known
  176. '   about that State
  177.     If Not pvtState Is Nothing Then
  178.         ReturnString = ReturnString & pvtState.StateCode
  179.     ElseIf StateCode > "" Then
  180.         ReturnString = ReturnString & pvtStateCode
  181.     End If
  182.     
  183.     If ZipCode >= 0 Then
  184.         ReturnString = ReturnString & " " & FormattedZip
  185.     End If
  186.     
  187. '' show-off a little by revealing interesting information
  188. ''   available only through having a rich object
  189. ''   model
  190. '    If Not pvtState Is Nothing Then
  191. '        ReturnString = _
  192. '            pvtContatenateWithCRLF( _
  193. '                String1:=ReturnString, _
  194. '                String2:="(State's Capital is " & _
  195. '                    pvtState.CapitalCity & _
  196. '                ")")
  197. '    End If
  198.     
  199.     FormattedAddress = ReturnString
  200. End Function
  201.  
  202. Private Function pvtContatenateWithCRLF(Optional String1 As Variant, Optional String2 As Variant) As String
  203.  
  204.     Dim ReturnString As String
  205.     
  206.     ReturnString = ""
  207.     
  208.     If Not IsMissing(String1) Then
  209.         ReturnString = String1
  210.         If String1 > "" Then
  211.             ReturnString = ReturnString & vbCrLf
  212.         End If
  213.     End If
  214.     
  215.     If Not IsMissing(String2) Then
  216.         If String2 > "" Then
  217.             ReturnString = ReturnString & String2
  218.         End If
  219.     End If
  220.  
  221.     pvtContatenateWithCRLF = ReturnString
  222. End Function
  223. Public Function FormattedZip() As String
  224.  
  225.     Dim ReturnString As String
  226.     
  227.     ReturnString = ""
  228.     
  229.     If ZipCode >= 0 Then
  230.         ReturnString = ReturnString & Format$(ZipCode, "00000")
  231.     End If
  232.     
  233.     If ZipSupplement > 0 Then
  234.         If ReturnString = "" Then
  235.             ReturnString = Format$(ZipSupplement, "0000")
  236.         Else
  237.             ReturnString = ReturnString & "-" & Format$(ZipSupplement, "0000")
  238.         End If
  239.     End If
  240.     
  241.     If ZipExtension > 0 Then
  242.         If ReturnString = "" Then
  243.             ReturnString = Format$(ZipExtension, "00")
  244.         Else
  245.             ReturnString = ReturnString & "-" & Format$(ZipExtension, "00")
  246.         End If
  247.     End If
  248.  
  249.     FormattedZip = ReturnString
  250. End Function
  251.  
  252.  
  253. Public Function SetStateFromStateCode(StateCode As String) As Variant
  254. ' Convert the StateCode value to a State object
  255.  
  256.     Dim NewState As New State
  257.     
  258.     Set NewState = _
  259.         ObjectManager. _
  260.             NewObject( _
  261.                 Sample:=NewState, _
  262.                 WhereClause:="StateCode = '" & StateCode & "'")
  263.                 
  264.     If Not NewState Is Nothing Then
  265.         Set State = NewState
  266.         Set SetStateFromStateCode = NewState
  267.     Else
  268.         Set SetStateFromStateCode = Nothing
  269.     End If
  270.     
  271.     Set NewState = Nothing
  272. End Function
  273.  
  274. Public Property Get State() As State
  275.     Set State = pvtState
  276. End Property
  277.  
  278.  
  279. Public Property Set State(aState As State)
  280.     Set pvtState = aState
  281. End Property
  282.  
  283. Private Sub Class_Initialize()
  284.    
  285.     Set ObjectManager = Nothing
  286.     
  287.     ZipCode = -1
  288.     ZipSupplement = -1
  289.     ZipExtension = -1
  290.     ForeignZipCode = ""
  291.     Status = "Current"
  292.     Usage = "Primary"
  293.  
  294. End Sub
  295.  
  296.  
  297.  
  298.  
  299. Public Function ObjectInitializeFromRecordSet(Optional RecordSet As Variant) As Address
  300. ' Populate my variables from the RecordSet
  301. '   (in support of VBOFCollection)
  302.  
  303.     Dim NewState As New State
  304.  
  305.     On Local Error Resume Next
  306.     
  307.     Line1 = RecordSet("Line1")
  308.     Line2 = RecordSet("Line2")
  309.     Line3 = RecordSet("Line3")
  310.     City = RecordSet("City") & ""
  311.     ZipCode = RecordSet("ZipCode") + 0
  312.     ZipSupplement = RecordSet("ZipSupplement") + 0
  313.     ZipExtension = RecordSet("ZipExtension") + 0
  314.     ForeignZipCode = RecordSet("ForeignZipCode") & ""
  315.     Status = RecordSet("Status") & ""
  316.     Usage = RecordSet("Usage") & ""
  317.     
  318.     ObjectID = RecordSet("ObjectID")
  319.     
  320. ' pick-up the State object
  321.     If Not IsNull(RecordSet("StateObjectID")) Then
  322.         Set pvtState = _
  323.             ObjectManager. _
  324.                 NewObject( _
  325.                     Sample:=NewState, _
  326.                     ObjectID:=CStr(RecordSet("StateObjectID")))
  327.     End If
  328.  
  329.     Set ObjectInitializeFromRecordSet = Me
  330. End Function
  331.  
  332. Public Function ObjectListBoxValue() As String
  333. ' Return a String will represent this object
  334. '   in a ListBox
  335. '   (in support of VBOFCollection)
  336.  
  337.     ObjectListBoxValue = _
  338.         Line1 & " " & _
  339.         City & ", " & _
  340.         StateCode & " " & _
  341.         FormattedZip
  342.  
  343. End Function
  344.  
  345.  
  346. Public Function ObjectNewInstanceOfMyClass() As Address
  347. ' Return a new instance of this class
  348. '   (in support of VBOFCollection)
  349.  
  350.     Set ObjectNewInstanceOfMyClass = New Address
  351. End Function
  352.  
  353.  
  354.  
  355. Public Function ObjectInitializeRecordSet(Optional RecordSet As Variant) As Long
  356. ' Populate the RecordSet with my variables.
  357. ' Note:  Do not initialize the ObjectID column.
  358. ' Return any error code encountered.
  359. '   (in support of VBOFCollection)
  360.  
  361.     On Local Error GoTo InitializeRecordSet_SetError
  362.     Err = 0
  363.     
  364.     RecordSet("Line1") = Line1
  365.     RecordSet("Line2") = Line2
  366.     RecordSet("Line3") = Line3
  367.     RecordSet("City") = City
  368.     RecordSet("ZipCode") = ZipCode
  369.     RecordSet("ZipSupplement") = ZipSupplement
  370.     RecordSet("ZipExtension") = ZipExtension
  371.     RecordSet("ForeignZipCode") = ForeignZipCode
  372.     RecordSet("Status") = Status
  373.     RecordSet("Usage") = Usage
  374.     
  375. ' set the State object
  376.     If Not pvtState Is Nothing Then
  377.         RecordSet("StateObjectID") = _
  378.             pvtState.ObjectID
  379.     Else
  380.         RecordSet("StateObjectID") = Null
  381.     End If
  382.     
  383. ' Note:  Do not initialize the ObjectID column.
  384.     
  385.     GoTo InitializeRecordSet_SetError
  386.  
  387. InitializeRecordSet_SetError:
  388.     ObjectInitializeRecordSet = Err
  389.     Exit Function
  390. End Function
  391.  
  392. Private Sub Class_Terminate()
  393.     If Not ObjectManager Is Nothing Then
  394.         ObjectManager.TerminateObject _
  395.             Object:=Me
  396.     End If
  397. End Sub
  398.  
  399. Public Property Get Line1() As String
  400.     Line1 = pvtLine1
  401. End Property
  402.  
  403. Public Property Let Line1(aString As String)
  404.     pvtLine1 = aString
  405. '    ObjectHasChanged
  406. End Property
  407.  
  408. Public Property Get Line2() As String
  409.     Line2 = pvtLine2
  410. End Property
  411.  
  412. Public Property Let Line2(aString As String)
  413.     pvtLine2 = aString
  414. '    ObjectHasChanged
  415. End Property
  416.  
  417. Public Property Get Line3() As String
  418.     Line3 = pvtLine3
  419. End Property
  420.  
  421. Public Property Let Line3(aString As String)
  422.     pvtLine3 = aString
  423. '    ObjectHasChanged
  424. End Property
  425.  
  426. Public Property Get City() As String
  427.     City = pvtCity
  428. End Property
  429.  
  430. Public Property Let City(aString As String)
  431.     pvtCity = aString
  432. '    ObjectHasChanged
  433. End Property
  434.  
  435. Public Property Get StateCode() As String
  436.     
  437.     If Not pvtState Is Nothing Then
  438.         StateCode = pvtState.StateCode
  439.     Else
  440.         StateCode = pvtStateCode
  441.     End If
  442. End Property
  443.  
  444.  
  445. Public Property Let StateCode(aString As String)
  446. ' StateCode is not really stored in the Address
  447. '   object (that would be too conventional.)  To
  448. '   expoit the power of OO, the Address object
  449. '   stores a reference to the entire State object,
  450. '   not just its StateCode.
  451. ' So, aString must be converted intoa State object
  452.  
  453.     SetStateFromStateCode (aString)
  454. End Property
  455.  
  456. Public Property Get ZipCode() As Long
  457.     ZipCode = pvtZipCode
  458. End Property
  459.  
  460. Public Property Let ZipCode(aLong As Long)
  461.     pvtZipCode = aLong
  462. '    ObjectHasChanged
  463. End Property
  464.  
  465. Public Property Get ZipSupplement() As Integer
  466.     ZipSupplement = pvtZipSupplement
  467. End Property
  468.  
  469. Public Property Let ZipSupplement(anInteger As Integer)
  470.     pvtZipSupplement = anInteger
  471. '    ObjectHasChanged
  472. End Property
  473.  
  474. Public Property Get ZipExtension() As Integer
  475.     ZipExtension = pvtZipExtension
  476. End Property
  477.  
  478. Public Function ZipExtensionString() As String
  479.     If pvtZipExtension >= 0 Then
  480.         ZipExtensionString = pvtZipExtension
  481.     Else
  482.         ZipExtensionString = ""
  483.     End If
  484. End Function
  485.  
  486. Public Function ZipSupplementString() As String
  487.     If pvtZipSupplement >= 0 Then
  488.         ZipSupplementString = pvtZipSupplement
  489.     Else
  490.         ZipSupplementString = ""
  491.     End If
  492. End Function
  493.  
  494.  
  495. Public Property Let ZipExtension(anInteger As Integer)
  496.     pvtZipExtension = anInteger
  497. '    ObjectHasChanged
  498. End Property
  499.  
  500. Public Property Get ForeignZipCode() As String
  501.     ForeignZipCode = pvtForeignZipCode
  502. End Property
  503.  
  504. Public Property Let ForeignZipCode(aString As String)
  505.     pvtForeignZipCode = aString
  506. '    ObjectHasChanged
  507. End Property
  508.  
  509. Public Property Get Status() As String
  510.     Status = pvtStatus
  511. End Property
  512.  
  513. Public Property Let Status(aString As String)
  514.     pvtStatus = aString
  515. '    ObjectHasChanged
  516. End Property
  517.  
  518. Public Property Get Usage() As String
  519.     Usage = pvtUsage
  520. End Property
  521.  
  522. Public Property Let Usage(aString As String)
  523.     pvtUsage = aString
  524. '    ObjectHasChanged
  525. End Property
  526.  
  527. Public Function ObjectHasChanged()
  528. ' Mark this object as "Changed" and trigger the
  529. '   "Changed" event
  530.  
  531.     On Local Error Resume Next
  532.  
  533.     ObjectChanged = True
  534.     
  535. #If NoEventMgr = False Then
  536.     If Not ObjectManager Is Nothing Then
  537.         ObjectManager. _
  538.             TriggerObjectEvent _
  539.                 Event:="Changed", _
  540.                 Object:=Me
  541.     End If
  542. #End If
  543. End Function
  544.  
  545.  
  546.  
  547. Public Function ObjectEventCallBack(Optional Event As Variant, Optional Object As Variant) As Long
  548. ' Receive the Trigger notification and process
  549. '   accordingly
  550. '
  551. ' Parameters:
  552. '   Event
  553. '       a string which identifies the Event
  554. '       Example: "Changed", "Created", "Deleted"
  555. '   Object
  556. '       the object originating the Event.
  557. '       responds to:
  558. '           TypeName(TriggerObject)
  559. '           TriggerObject.ObjectID
  560. ' (supported by VBOFEventManager)
  561.  
  562. End Function
  563.  
  564.  
  565.  
  566.