home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.visual.basic      MS Visual Basic discussions, NOT dot-net      10,840 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 9,468 of 10,840   
   Steve Gerrard to Iain Bishop   
   Re: Object Model Problem   
   03 May 05 19:24:22   
   
   From: mynamehere@comcast.net   
      
   "Iain Bishop"  wrote in message   
   news:CtSde.1925$31.14@news-server.bigpond.net.au...   
   > Thanks a lot Dikkie and Steve for your responses. Its very much appreciated.   
   > I'm beginning to get an idea of what I need to do now.   
   >   
   > Steve, you mentioned interfaces. I know of them but have never used them, so   
   > am not familiar with them or how they could help. Would you mind explaining   
   > how I could use them?   
   >   
   > Thanks   
   > Iain   
   >   
      
   I have to admit, I was kind of hoping you would ask :)   
      
   Here is a demo, which hopefully illustrates the concept. I have tried to keep   
   it   
   as brief as possible, there are lots of things you might want to add if you   
   decided to pursue it.   
      
   You need one form with a command button, and three classes, named as given.   
   Paste the code into each one, then run it and click the button. You may want to   
   trace the execution to see what is happening, especially the GetTotal call at   
   the end of the button click. Note that clicking the button again will add more   
   stuff.   
      
   '----   
   ' first, the interface, a class called IMaterial   
      
   Public Property Get MaterialTotal() As Double   
   End Property   
      
   '----   
   ' that was easy enough. Now the first instance class.   
   ' this is CMaterial, for an individual material item.   
   ' Instead of the usual property Get/Let, I just initialize to   
   ' random values to keep it short.   
      
   Implements IMaterial   
      
   Private mQuantity As Double   
   Private mCostPer As Double   
      
   ' class   
   Private Sub Class_Initialize()   
       mQuantity = Fix(Rnd * 10)   
       mCostPer = Fix(Rnd * 100)   
   End Sub   
      
   ' IMaterial implementation   
   Private Property Get IMaterial_MaterialTotal() As Double   
       IMaterial_MaterialTotal = mQuantity * mCostPer   
   End Property   
      
   '---   
   ' This is the other instance class, called CAssembly.   
   ' It contains a collection of IMaterial items.   
      
   Implements IMaterial   
      
   Private mCol As Collection   
      
   ' class   
   Private Sub Class_Initialize()   
       Set mCol = New Collection   
   End Sub   
      
   ' public methods   
   Public Function Add(Item As IMaterial) As IMaterial   
       mCol.Add Item   
       ' return Item, so New can be used in the call...   
       Set Add = Item   
   End Function   
      
   Public Function GetTotal() As Double   
       Dim dSum As Double   
       Dim iMat As IMaterial   
      
      
       ' Note how the GetTotal function just retrieves the   
       ' MaterialTotal of each item, regardless of whether   
       ' it is a material or another assembly...   
      
       For Each iMat In mCol   
           dSum = dSum + iMat.MaterialTotal   
       Next iMat   
      
       GetTotal = dSum   
      
   End Function   
      
   ' IMaterial implementation   
   Private Property Get IMaterial_MaterialTotal() As Double   
       IMaterial_MaterialTotal = GetTotal()   
   End Property   
      
   '---   
   ' And last, a Form to test it.   
      
   Private mSite As CAssembly   
      
   Private Sub Form_Load()   
       Set mSite = New CAssembly   
   End Sub   
      
   Private Sub Command1_Click()   
       Dim oAsm As CAssembly   
       Dim oMat As CMaterial   
       Dim oAsm2 As CAssembly   
      
       ' oMat is not actually used here, but you   
       ' would need it to set other properties   
      
       'add a mat to the site   
       Set oMat = mSite.Add(New CMaterial)   
      
       'add an assembly to the site   
       Set oAsm = mSite.Add(New CAssembly)   
       'add some mats to the assembly   
       Set oMat = oAsm.Add(New CMaterial)   
       Set oMat = oAsm.Add(New CMaterial)   
      
       'add an assembly to the assembly   
       Set oAsm2 = oAsm.Add(New CAssembly)   
       'add some mats to the nested assembly   
       Set oMat = oAsm2.Add(New CMaterial)   
       Set oMat = oAsm2.Add(New CMaterial)   
      
       'see what we've got   
       MsgBox "Total is " & mSite.GetTotal   
      
   End Sub   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca