Announcement

Collapse
No announcement yet.

New to inheritance, simple question

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

  • Edwin Knoppert
    replied
    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.

    Leave a comment:


  • Edwin Knoppert
    replied
    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:
    [color=#0000FF]Class[/color] clsImportObject
    
        [color=#0000FF]Interface[/color] iImportObject: [color=#0000FF]Inherit[/color] [color=#0000FF]IUnknown[/color]
        
            [color=#0000FF]Method[/color] GetImportName() [color=#0000FF]As[/color] [color=#0000FF]String[/color]
                [color=#0000FF]Method[/color] = "Unknown"
            [color=#0000FF]End[/color] [color=#0000FF]Method[/color]
    
        [color=#0000FF]End[/color] [color=#0000FF]Interface[/color]
    
    [color=#0000FF]End[/color] [color=#0000FF]Class[/color]
    
    [color=#0000FF]Class[/color] clsUNIT4AGRESSO
    
        [color=#0000FF]Interface[/color] UNIT4AGRESSO_iImportObject: [color=#0000FF]Inherit[/color] clsImportObject, iImportObject
        
            Override [color=#0000FF]Method[/color] GetImportName() [color=#0000FF]As[/color] [color=#0000FF]String[/color]
                [color=#0000FF]Method[/color] = "UNIT4AGRESSO"
            [color=#0000FF]End[/color] [color=#0000FF]Method[/color]    
    
        [color=#0000FF]End[/color] [color=#0000FF]Interface[/color]
    
    [color=#0000FF]End[/color] [color=#0000FF]Class[/color]
    
    [color=#0000FF]Macro[/color] Import_CreateObject(p1, p2)
        [color=#0000FF]MacroTemp[/color] i2
        [color=#0000FF]Dim[/color] i2 [color=#0000FF]As[/color] p2
        i2 = [color=#0000FF]Class[/color] p1
        i = [color=#0000FF]Nothing[/color]
        [color=#0000FF]If[/color] [color=#0000FF]IsObject[/color]( i2 ) [color=#0000FF]Then[/color]
            [color=#0000FF]Poke[/color] [color=#0000FF]Dword[/color], [color=#0000FF]VarPtr[/color]( i ), [color=#0000FF]ObjPtr[/color]( i2 )
            i.Addref()
            i2 = [color=#0000FF]Nothing[/color]
        [color=#0000FF]End[/color] [color=#0000FF]If[/color]
    [color=#0000FF]End[/color] [color=#0000FF]Macro[/color]
    
    [color=#0000FF]Function[/color] Test( [color=#0000FF]ByVal[/color] nIndex [color=#0000FF]As[/color] [color=#0000FF]Long[/color] ) [color=#0000FF]As[/color] [color=#0000FF]Long[/color]
    
        [color=#0000FF]Local[/color] i [color=#0000FF]As[/color] iImportObject
    
        [color=#0000FF]Select[/color] [color=#0000FF]Case[/color] nIndex
        [color=#0000FF]Case[/color] 0: i = [color=#0000FF]Class[/color] "clsImportObject"    
        [color=#0000FF]Case[/color] 1: Import_CreateObject( "clsUNIT4AGRESSO", UNIT4AGRESSO_iImportObject )
        [color=#0000FF]Case[/color] [color=#0000FF]Else[/color]
            [color=#0000FF]Exit[/color] [color=#0000FF]Function[/color]
        [color=#0000FF]End[/color] [color=#0000FF]Select[/color]
    
        [color=#0000FF]If[/color] [color=#0000FF]IsNothing[/color]( i ) [color=#0000FF]Then[/color] [color=#0000FF]Exit[/color] [color=#0000FF]Function[/color]
    
        [color=#0000FF]MsgBox[/color] i.GetImportName()
        
        [color=#007F00]' More i... calls here....[/color]
    
    [color=#0000FF]End[/color] [color=#0000FF]Function[/color]

    Leave a comment:


  • jcfuller
    replied
    Edwin,
    That's the only way I know to do it.

    James

    Leave a comment:


  • Edwin Knoppert
    replied
    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"    
        [B]Poke Dword, VarPtr( i1 ), ObjPtr( i2 )
        i1.addref()[/B]
    
        MsgBox i1.TEST() & ", " & i2.TEST()
    
    End Function

    Leave a comment:


  • Brian Chirgwin
    replied
    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

    Leave a comment:


  • Edwin Knoppert
    replied
    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?

    Leave a comment:


  • José Roca
    replied
    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.

    Leave a comment:


  • Edwin Knoppert
    started a topic New to inheritance, simple question

    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.
Working...
X