No announcement yet.

What is the benefit of com-interfaces?

  • Filter
  • Time
  • Show
Clear All
new posts

  • What is the benefit of com-interfaces?

    For most classes i would not have a 2nd or more interface.
    The 2nd can imo never be instantiated in the first class and therefore the instance variables are not shared.

    I would like to have a com interface in an existing interface but so far i don't see how and use (odd) references.

    I guess com-interfaces are really implemented like this to offer different versions while underlaying functionality may differ only little (or a lot but that's not the purpose).

    To be clear, a 2nd interface is always created in a new object right?
    Thus no sharing existing data right?

  • #2
    You can have as many IUnknown interfaces as you wish in the same class, but only one dual interface.
    Last edited by José Roca; 2 Dec 2008, 12:49 PM.


    • #3
      Two interfaces declared in the same class will (still) never share the instance data right?


      • #4
        Not right.


        • #5
          A ha!

              Local oInterface1 As Interface1
              oInterface1 = Class "Class1"
              Local oInterface2 As Interface2
              oInterface1.DateValue = "1234"
              oInterface2 = oInterface1
              MsgBox oInterface2.DateValueInt2
          It seems to work fine, am i doing it right?

          If so, this would really apply to my other topic:

          (Partly though..)


          • #6
            It seems to work fine, am i doing it right?
            Yes. When you have more than one interface in a class, you just have to call QueryInterface to navigate between them. oInterface2 = oInterface1
            implicitily calls QueryInterface.

            If so, this would really apply to my other topic
            No. Circular references should be avoided at all cost.
            Last edited by José Roca; 2 Dec 2008, 12:47 PM.


            • #7
              Yes i know and will but i mean tracking a 'parent' (or it's data) is much easier this way.
              This code makes com programming much more easier and interesting.



              • #8
                If you need them to be compatible with Automation compilers such VB6 and .NET, you can use INHERIT IAutomation. You can have has many interfaces as you wish in the class and still be usable with VB. However, scripting languages such VBScript need dispatch interfaces.


                • #9
                  Meaning: only one interface for those isn't?

                  While it's good to know, i'll be using the IUnknown interface for most of my work.
                  In a 'dual' case i may need to write me a wrapper class or so.
                  (don't see any use for me anyway at this time)


                  • #10
                    I have worked it out but i have a question.
                    Will obtaining the interface like i did below be a problem (if i use it a lot)?
                    Frankly i think this is the better thing to do, the object returned will be terminated right away.
                    My debug shows the destruction of the class and therefore no circular reference.
                    Iow, looks good!

                    Class ParentClass
                        Instance m_Value As String
                        Interface IParentClass: Inherit IUnknown
                            Property Get Items As iItems
                                [B]Property = Me[/B]
                            End Property
                            Property Get TheDate As String
                                Property = Date$
                            End Property
                        End Interface
                        Interface iItems: Inherit IUnknown
                            Property Get Parent As IParentClass
                                [B]Property = Me[/B]
                            End Property
                            Property Get Value() As String
                                Property = m_Value
                            End Property
                            Property Set Value( ByVal Value As String )
                                m_Value = Value
                            End Property
                        End Interface
                    End Class
                        Local o As IParentClass
                        o = Class "ParentClass"
                        'Local i As iItems
                        'i = o.Items
                        'i.Value = Time$
                        o.Items.Value = Time$
                        MsgBox o.Items.Value & ", " & o.Items.Parent.TheDate
                    Last edited by Edwin Knoppert; 3 Dec 2008, 02:47 AM.


                    • #11
                      Just added a parent property as well
                      Still don't know how to deal with multiple items being maintained by iItems.
                      (Items not shown here)


                      • #12
                        No problem, although it adds some overhead. In each call, the compiler has to create a temporary object variable, call QueryInterface and later release it.


                        • #13
                          Yes, but maybe this is the way to go.

                          Still messing with the items themselves.
                          It must be able to use any additional class thus outside this parentclass anyway.
                          I was wondering how this was done with a .NET class like

                          ParentClass p = new ParentClass()
                          Items it = p.Items
                          Item i = it[0]

                          it = null
                          p = null
                          So.. what about i ?


                          • #14
                            PB does not currently support collections.


                            • #15
                              I know, the question was about something else.

                              However, i have placed a piece of code on my board.
                              It shows the relationship issue and maintaining an items collection.
                              At this time Array ... calls don't seem to apply to interfaces so i used old fashion redim and copy and such.

                              Great domain names provide SEO, branding, and a memorable experience for your users. Get a premium domain today.


                              • #16
                                Hi Edwin,

                                Here is code that I wrote when I was playing with the parent/child relationship for PB objects. It is a simple one Company with many Customers relationship. It is a little different than yours in that it creates the child array in the CREATE method and assumes that it will be one based. Also, I return a reference to the child object when it is added to the parent and then assign the child properties, versus creating a child object, filling in its properties and then assigning it to the parent. Also, the code is not as complete as yours because I haven't included anything to remove one or all of the child items (that should be easy enough to do though).

                                A Collections object would be nice....

                                #Compile Exe
                                Class clsCustomer
                                   Instance m_sCustID   As String
                                   Instance m_sCustName As String
                                   Instance m_nCustBal  As Currency
                                   Interface CustomerInterface: Inherit IUnknown     
                                      Property Get CustID() As String
                                         Property = m_sCustID
                                      End Property
                                      Property Set CustID( ByVal sCustID As String ) 
                                         m_sCustID = sCustID
                                      End Property
                                      Property Get CustName() As String
                                         Property = m_sCustName
                                      End Property
                                      Property Set CustName( ByVal sCustName As String ) 
                                         m_sCustName = sCustName
                                      End Property
                                      Property Get CustBal() As Currency
                                         Property = m_nCustBal
                                      End Property
                                      Property Set CustBal( ByVal nCustBal As Currency ) 
                                         m_nCustBal = nCustBal
                                      End Property
                                   End Interface
                                End Class
                                Class clsCompany
                                   Instance m_pCustomers()   As CustomerInterface
                                   Instance m_nCustomerCount As Long
                                   Instance m_sCompanyName   As String
                                   Instance m_sCompanyID     As String
                                   Class Method Create()
                                      ' Initialize the customer array
                                      Dim m_pCustomers(0) As Instance CustomerInterface
                                   End Method
                                   Interface CompanyInterface: Inherit IUnknown     
                                      Property Get CompanyName() As String
                                         Property = m_sCompanyName
                                      End Property
                                      Property Set CompanyName( ByVal sCompanyName As String ) 
                                         m_sCompanyName = sCompanyName
                                      End Property
                                      Property Get CompanyID() As String
                                         Property = m_sCompanyID 
                                      End Property
                                      Property Set CompanyID( ByVal sCompanyID As String ) 
                                         m_sCompanyID = sCompanyID
                                      End Property
                                      Property Get CustomerCount() As Long
                                         Property = m_nCustomerCount
                                      End Property
                                      Method AddCustomer() As CustomerInterface
                                         Incr m_nCustomerCount
                                         ReDim Preserve m_pCustomers( m_nCustomerCount ) As Instance CustomerInterface
                                         m_pCustomers(m_nCustomerCount) = Class "clsCustomer"
                                         ' Return a reference to the new customer object
                                         Method = m_pCustomers(m_nCustomerCount)  
                                      End Method
                                      Method GetCustomer( ByVal nPosition As Long ) As CustomerInterface
                                         Method = m_pCustomers( nPosition )
                                      End Method
                                   End Interface
                                End Class
                                '//  SAMPLE PROGRAM
                                Function PBMain() As Long
                                   Local cCompany As CompanyInterface
                                   Local cCust    As CustomerInterface
                                   Local y        As Long
                                   ' Create a new company
                                   cCompany = Class "clsCompany"
                                   ' Add info to our new company
                                   cCompany.CompanyName = "PlanetSquires Software"
                                   cCompany.CompanyID   = "PLANETSQ"
                                   ' Add two new customers to the company
                                   cCust = cCompany.AddCustomer()
                                   cCust.CustID   = "1000"
                                   cCust.CustName = "Paul Squires"
                                   cCust.CustBal  = 1000.99
                                   cCust = cCompany.AddCustomer()
                                   cCust.CustID   = "2000"
                                   cCust.CustName = "Tammy Squires"
                                   cCust.CustBal  = 2000.99
                                   ' Display our Company information
                                   ? "Company Name: " & cCompany.CompanyName & $CrLf & _
                                     "Company ID:   " & cCompany.CompanyID
                                   ' Display our Customer information
                                   For y = 1 To cCompany.CustomerCount
                                      cCust = cCompany.GetCustomer(y)
                                      If IsObject(cCust) Then
                                         ? cCust.CustName & $CrLf & _
                                           cCust.CustID   & $CrLf & _
                                           Format$(cCust.CustBal, "$#.00")
                                      End If
                                   ? "done"
                                End Function
                                Paul Squires
                                FireFly Visual Designer (for PowerBASIC Windows 10+)
                                Version 3 now available.


                                • #17
                                  You can build collections using the Dictionary object.


                                  • #18
                                    Thanks José - that's a good point. Simply add the dictionary to the class to handle all children objects. I think that I'll do that.
                                    Paul Squires
                                    FireFly Visual Designer (for PowerBASIC Windows 10+)
                                    Version 3 now available.


                                    • #19
                                      MS Data Shaping OLE provider (with ADO) was designed to support hierachical data relationships.

                                      No, I never tried it, but just this week I received my O'Reilly book on ADO and found that whilst browsing it.
                                      Michael Mattias
                                      Tal Systems (retired)
                                      Port Washington WI USA
                                      [email protected]