Announcement

Collapse
No announcement yet.

New to inheritance, simple question

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • New to inheritance, simple question

    This seems to work fine:

    Code:
    Class Class1
    
        Interface Class1_Interface1: Inherit IUnknown
    
            Method TEST() As String
                Method = "1234"
            End Method
    
        End Interface
    
    End Class
    
    Class Class2
    
        Interface Class2_Interface1: Inherit Class1, Class1_Interface1
    
            Override Method TEST() As String
                Method = "2345"
            End Method
    
        End Interface
    
    End Class
    
    Function Test() As Long
    
        Local i1 As Class1_Interface1
        Local i2 As Class2_Interface1
        
        i1 = Class "Class1"
        i2 = Class "Class2"    
    
        MsgBox i1.TEST() & ", " & i2.TEST()
    
    End Function
    Question is that i have a single variable of a specific interface type where i want to refer to the class created.
    Like:
    Code:
    Function Test() As Long
    
        Local i2 As Class2_Interface1
        Local i As Class1_Interface1
        
        i2 = Class "Class2"    
        i = i2
    
        MsgBox i.TEST()
    
    End Function
    So that i get's independent from the actual type.
    In practice i would like to do this:
    Code:
    Function Test() As Long
    
        Local i As Class1_Interface1
        i = Class "Class2"    
        MsgBox i.TEST()
    
    End Function
    The above fails but is my logic flawed?
    If not should store the interface in an IUnknown type?
    IUnknown is afaik not mentioned in the help to be a variable type.
    hellobasic

  • #2
    Code:
        Local i As Class1_Interface1
        i = Class "Class2"
    The above fails but is my logic flawed?
    Yes. There is not a Class1_Interface1 in Class2. This is COM, not C++, Java or wathever.

    Either use:

    Code:
        Local i As Class1_Interface1
        i = Class "Class1"
    or:

    Code:
        Local i As Class2_Interface1
        i = Class "Class2"
    If not should store the interface in an IUnknown type?
    IUnknown is afaik not mentioned in the help to be a variable type.
    It is not a variable type. It is an interface name.
    Forum: http://www.jose.it-berater.org/smfforum/index.php

    Comment


    • #3
      The point is that i need to hold a reference to these kind of interfaces in a single variable so i can call them using the same variable name and functionset while internally the interfaces handle their code differently..

      Iow ordinary inheritance stuff but what var type shall use to be able to hold any of the interfaces described?
      hellobasic

      Comment


      • #4
        non random GUIDs?

        Originally posted by Edwin Knoppert View Post
        The point is that i need to hold a reference to these kind of interfaces in a single variable so i can call them using the same variable name and functionset while internally the interfaces handle their code differently..

        Iow ordinary inheritance stuff but what var type shall use to be able to hold any of the interfaces described?
        Great question, I was just working on this. I have a partial answer, but not sure if I am correct.

        The following code works, but am I asking for trouble. If this is correct, it seems PB should automatically assign the same GUID (not random) in when using an inherit class, interface.

        Can anyone confirm that this a valid approach or is this asking for trouble (assigning the same GUID to different interfaces)?

        Code:
        #Compile Exe
        #Dim All
        
        ' Note: These guids taken from help file and not gauranteed to be unique
        ' for demo purposes only
        
        $cPerson = Guid$("{00000099-0000-0000-0000-000000000008}")
        $iPerson = Guid$("{00000099-0000-0000-0000-000000000009}")
        $cEmployee = Guid$("{00000099-0000-0000-0000-000000000010}")
        $iEmployee = Guid$("{00000099-0000-0000-0000-000000000009}") ' NOTE same as iPerson
        
        Class cPerson $cPerson
            Instance sLastName As String
            Instance sFirstName As String
            Instance sAddress As String
            Instance sCity As String
            Instance sState As String
            Instance sZip As String
        
            Interface iPerson $iPerson
                Inherit IUnknown
        
                Property Get Lastname () As String
                    Property = sLastname
                End Property
        
                Property Set Lastname(ByVal lName As String)
                    sLastname = lname
                End Property
        
                Property Get Firstname() As String
                    Property = sFirstName
                End Property
        
                Property Set firstName(ByVal fName As String)
                    sFirstname = fname
                End Property
        
                Property Get Address() As String
                    Property = sAddress
                End Property
        
                Property Set Address(ByVal sAddr As String)
                    sAddress = sAddr
                End Property
        
                Property Get City() As String
                    Property = sCity
                End Property
        
                Property Set City (ByVal sNewCity As String)
                    sCity = sNewCity
                End Property
        
                Property Get State() As String
                    Property = sState
                End Property
        
                Property Set State(ByVal sNewState As String)
                    sState = sNewState
                End Property
        
                Property Get Zip() As String
                    Property = sZip
                End Property
        
                Property Set Zip(ByVal sNewZip As String)
                    sZip = sNewZip
                End Property
        
                Method FullName() As String
                    Method = sFirstname + " " + sLastname
                End Method
        
                Method LastNameCommaFirst() As String
                    Method = sLastname + ", " + sFirstname
                End Method
        
                Method AddressLabel() As String
                    Method = me.FullName + $CrLf + me.Address + $CrLf + me.City + ", " + me.State + " " + me.Zip
                End Method
        
            End Interface
        
        End Class
        
        Class cEmployee $cEmployee
            Instance sTitle As String
            Instance sSalary As String
        
            Interface iEmployee $iEmployee
                Inherit cPerson, iPerson
        
                Override Method AddressLabel() As String
                    Method = Me.Title + " " + me.FullName + $CrLf + me.Address + $CrLf + me.City + ", " + me.State + " " + me.Zip
                End Method
        
                Property Get Title () As String
                    Property = sTitle
                End Property
        
                Property Set Title (ByVal newTitle As String)
                    sTitle = newTitle
                End Property
        
            End Interface
        
        End Class
        
        Function printAddress (obj As iPerson) As String
            Function = obj.addresslabel
        End Function
        
        Function PBMain () As Long
        
            Dim persons(10) As iPerson
        
            Local i As Long
            Local person As iPerson
        
            Local person2 As iEmployee
        
            For i = 0 To 10
                If i Mod 2 = 0 Then
        
                    person = Class "cPerson"
                    person.firstname = "John"
                    person.lastname = "Smith"
                    person.address = "123 Main Street"
                    person.city = "Anytown"
                    person.state = "Anywhere"
                    person.zip = "12345"
        
                    persons(i) = person
        
                Else
                    person2 = Class "cEmployee"
                    person2.Title = "President"
                    person2.firstname = "Jane"
                    person2.lastname = "Doe"
                    person2.address = "123 Main Street"
                    person2.city = "Anytown"
                    person2.state = "Anywhere"
                    person2.zip = "12345"
                    persons(i) = person2
                End If
            Next i
        
            For i = 0 To 10
                MsgBox printaddress(persons(i))
            Next i
        
        
        '    MsgBox person.FullName & $CrLf & person2.fullname
        '    MsgBox person.LastnameCommaFirst & $CrLf & person2.LastnameCommaFirst
        '    MsgBox person.addresslabel
        
        End Function

        Comment


        • #5
          While it works, i would like to avoid this kind of code:
          Code:
          Class Class1
          
              Class Method Destroy
                  MsgBox "Destroy Class1"
              End Method
          
              Interface Class1_Interface1: Inherit IUnknown
          
                  Method TEST() As String
                      Method = "1234"
                  End Method
          
              End Interface
          
          End Class
          
          Class Class2
          
              Class Method Destroy
                  MsgBox "Destroy Class2"
              End Method
          
              Interface Class2_Interface1: Inherit Class1, Class1_Interface1
          
                  Override Method TEST() As String
                      Method = "2345"
                  End Method
          
              End Interface
          
          End Class
          
          Function Test() As Long
          
              Local i1 As Class1_Interface1
              Local i2 As Class2_Interface1
              
              i2 = Class "Class2"    
              Poke Dword, VarPtr( i1 ), ObjPtr( i2 )
              i1.addref()
          
              MsgBox i1.TEST() & ", " & i2.TEST()
          
          End Function
          hellobasic

          Comment


          • #6
            Edwin,
            That's the only way I know to do it.

            James

            Comment


            • #7
              That's not so nice, fortunately i just discovered PB checks the sequence of it's methods so it maybe suitable to move the pointer to another typedef.

              Here is a better example what i need:

              Code:
              Class clsImportObject
              
                  Interface iImportObject: Inherit IUnknown
                  
                      Method GetImportName() As String
                          Method = "Unknown"
                      End Method
              
                  End Interface
              
              End Class
              
              Class clsUNIT4AGRESSO
              
                  Interface UNIT4AGRESSO_iImportObject: Inherit clsImportObject, iImportObject
                  
                      Override Method GetImportName() As String
                          Method = "UNIT4AGRESSO"
                      End Method    
              
                  End Interface
              
              End Class
              
              Macro Import_CreateObject(p1, p2)
                  MacroTemp i2
                  Dim i2 As p2
                  i2 = Class p1
                  i = Nothing
                  If IsObject( i2 ) Then
                      Poke Dword, VarPtr( i ), ObjPtr( i2 )
                      i.Addref()
                      i2 = Nothing
                  End If
              End Macro
              
              Function Test( ByVal nIndex As Long ) As Long
              
                  Local i As iImportObject
              
                  Select Case nIndex
                  Case 0: i = Class "clsImportObject"    
                  Case 1: Import_CreateObject( "clsUNIT4AGRESSO", UNIT4AGRESSO_iImportObject )
                  Case Else
                      Exit Function
                  End Select
              
                  If IsNothing( i ) Then Exit Function
              
                  MsgBox i.GetImportName()
                  
                  ' More i... calls here....
              
              End Function
              hellobasic

              Comment


              • #8
                Same problems arrise if you try to return a different interface like:
                Code:
                        Property Get SQL() As ImportObject_ISQL
                            Property = Me
                        End Property
                And:
                Code:
                        Override Property Get SQL() As UNIT4AGRESSO_ISQL
                            Property = Me
                        End Property
                Hmm, rather complex at this time for me.
                I know doable but so far not with strong typed coding techniques i guess.
                hellobasic

                Comment

                Working...
                X