Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using OOP in Visual BASIC w/ some lite XML. 3

Status
Not open for further replies.

Snaggs

Programmer
Jan 11, 2000
393
US
...This is a carry over request from a previous thread in the OOP forum.<br><br>Our business deals with the Electric Utility industry.&nbsp;&nbsp;Our object model consists of some different objects like<br><br>Call, Consumer & Outage to name a few.<br><br>A Call is an object (although some will argue this point).&nbsp;&nbsp;Using the methods described is VB6 Business Objects from Wrox Publishing, each one of these objects also has an corresponding collection, such as Calls, Consumers and Outages.<br><br>When we want to get a list of calls that a consumer has made we do something like this:<br><br>Set colCalls = New CCalls<br>Set objConsumer = New CConsumer<br>objConsumer.ConsumerID = &quot;123&quot;&nbsp;&nbsp;&nbsp;'Or whatever their number is.<br><br>If Not colCalls.Load(objConsumer) Then<br>MsgBox &quot;Error Loading Consumers Calls&quot;<br>EndIf<br><br>Then we use this to enumerate through the collection:<br><br>For Each objCall In colCalls<br>lstComment.Add objCall.CallerComment<br>Next objCall<br><br>The really cool thing about this is that you can pass different objects to the Calls collection.&nbsp;&nbsp;If we wanted to see a list of call that were made based on a specific outage we would do this:<br><br>If Not colCalls.Load(objOutage) Then<br>MsgBox &quot;Error Loading Calls for Outage&quot;<br>EndIf<br><br>In the Load event of the collection we use something called RTTI, Run Time Type Identification using the TypeName(object) statement in VB.&nbsp;&nbsp;This is how it knows what object got passed to it.&nbsp;&nbsp;The whole idea of this methodology was to be able to pass objects to objects.&nbsp;&nbsp;The Load method looks something like this:<br><br>&nbsp;&nbsp;&nbsp;&nbsp;Select Case UCase$(TypeName(objData))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Case &quot;CCONSUMER&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Load = Consumers(objData)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Case &quot;COUTAGE&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Load = Outages(objData)<br>&nbsp;&nbsp;&nbsp;&nbsp;End Select<br><br>Of course you can't use early binding here unless you had a base class that was implemented.&nbsp;&nbsp;We looked at that, but it was just another level down where you had to do the RTTI and put the intelligence in the wrong class.&nbsp;&nbsp;We axed it at that level and did it here.<br><br>Our object makes requests to a server by sending the XML over a Winsock connection.&nbsp;&nbsp;The server responds with XML and then it's parsed (properly) and placed in the collection.&nbsp;&nbsp;Since this information is collected in the Calls Collection, here's a little clip to show you how we Load the Call Collection based on the ConsumerID:<br><br>&nbsp;&nbsp;&nbsp;&nbsp;'Build the XML request.<br>&nbsp;&nbsp;&nbsp;&nbsp;strSendBuffer = &quot;&lt;Action&gt;&quot; & vbCrLf & _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vbTab & &quot;&lt;ActionID&gt;&quot; & enuActionID.lLISTCONSUMERCALLS & &quot;&lt;/ActionID&gt;&quot; & vbCrLf & _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vbTab & &quot;&lt;Consumer&gt;&quot; & vbCrLf & _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vbTab & vbTab & &quot;&lt;ConsumerID&gt;&quot; & objData.ConsumerID & &quot;&lt;/ConsumerID&gt;&quot; & vbCrLf & _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vbTab & &quot;&lt;/Consumer&gt;&quot; & vbCrLf & _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;&lt;/Action&gt;&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;'Create an instance of the winsock object<br>&nbsp;&nbsp;&nbsp;&nbsp;Set objWinsock = New CWinsock<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;'Send our XML request to the server.<br>&nbsp;&nbsp;&nbsp;&nbsp;objWinsock.SendData strSendBuffer<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;'Wait for a completed response.<br>&nbsp;&nbsp;&nbsp;&nbsp;strReceiveBuffer = objWinsock.ReceiveData<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;'Destroy the object.<br>&nbsp;&nbsp;&nbsp;&nbsp;Set objWinsock = Nothing<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;'Make sure something came back from the server.<br>&nbsp;&nbsp;&nbsp;&nbsp;If Len(strReceiveBuffer) &gt; 0 Then<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Create XML Document for Data Retrieval.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Set objDOM = New DOMDocument<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Load data from XML file<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If objDOM.loadXML(strReceiveBuffer) Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'List create of XML nodes<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Set objList = objDOM.selectNodes(&quot;//DateAndTime&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Iterate through list of nodes and populate the Calls collection<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;For intLoop = 0 To objList.length - 1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Create a call object.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Set objCall = New CCall<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Set the Call properties.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;With objCall<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Some XML tags may not be present.&nbsp;&nbsp;For those that are missing an error is generated<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'during the read.&nbsp;&nbsp;Resume next is used here to allow the reading of the next tag.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;On Error Resume Next<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.CallID = objDOM.getElementsByTagName(&quot;CallID&quot;).Item(intLoop).nodeTypedValue<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.CallerComment = objDOM.getElementsByTagName(&quot;CallerComment&quot;).Item(intLoop).nodeTypedValue<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.ConsumerID = objDOM.getElementsByTagName(&quot;ConsumerID&quot;).Item(intLoop).nodeTypedValue<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.DateAndTime = objDOM.getElementsByTagName(&quot;DateAndTime&quot;).Item(intLoop).nodeTypedValue<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.OperatorCode = objDOM.getElementsByTagName(&quot;OperatorCode&quot;).Item(intLoop).nodeTypedValue<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End With<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Add this call to a collection of calls.&nbsp;&nbsp;The collection was created in the class initialize.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;On Error GoTo ErrMsg<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s_colCalls.Add objCall&nbsp;&nbsp;', &quot;~&quot; & objCall.CallID<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Next<br><br>The way the XML was generated here is NOT the correct way to do it.&nbsp;&nbsp;It should have used the parser to append nodes to the document object model (DOM).&nbsp;&nbsp;This part will have to be redone, but you can get a feel for what the XML looks like.<br><br>As you can see the XML is pretty straight forward as is the object.&nbsp;&nbsp;This code you see above is never seen by the Application programmer.&nbsp;&nbsp;It's all encapsulated in the object.<br><br>This allows us to build a very thin client with very light network traffic.&nbsp;&nbsp;There are all sorts of other neat things you can do with this that I don't have time to go into, but use your imagination and I'm sure you'll think of many more uses for this.<br><br>For more information on XML, join the XML forum here at Tek-Tips!<br><br>Cheers, <p>Steve<br><a href=mailto:tribesaddict@swbell.net>tribesaddict@swbell.net</a><br><a href= > </a><br>
 
Steve,<br>&nbsp;&nbsp;&nbsp;Great post, but you should have used the tip icon instead of the questionmark. <p>nick bulka<br><a href=mailto: > </a><br><a href= > </a><br>
 
...It's the simple things in life that trip me up!&nbsp;&nbsp;Arggghh... <p>Steve<br><a href=mailto:tribesaddict@swbell.net>tribesaddict@swbell.net</a><br><a href= > </a><br>
 
Steve,<br><br>Many thanks for taking the time to help a fellow laborer.&nbsp;&nbsp;Makes me want to share whatever I have.<br> <p>John Kisner<br><a href=mailto:jlkisner@jlkisner.com>jlkisner@jlkisner.com</a><br><a href= > </a><br>
 
John,<br><br>Sometime back you helped me out when I had questions about your N-Tier system.&nbsp;&nbsp;You were more than helpful and friendly and some of the information I received from you was implemented in our project.&nbsp;&nbsp;Now's the time for me to return the favor!<br><br> <p>Steve<br><a href=mailto:tribesaddict@swbell.net>tribesaddict@swbell.net</a><br><a href= > </a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top