none
Generic contraints RRS feed

  • Question

  • Assuming I have the following interfaces:

    Public Interface IParty
       ' ...
    End Interface
    
    Public Interface IPerson
       Inherits IParty
       ' ...
    End Interface
    
    Public Interface IOrganization
       Inherits IParty
       . ...
    End Interface
    
    Public Interface IRelationshipType(Of PartyType1 as System.Type, PartyType2 as System.Type)
       ' ...
    End Interface

    How do I have to specify the constraints in the interface IRelationshipType in order to accept only IParty or one of its descendants as type?

    Many thanks & kind regards,

    Etienne

    Saturday, February 23, 2013 1:51 PM

Answers

  • Hi Etienne,

    Welcome to the MSDN Forum.

    Based on my understanding, this is not a kind of generic contraints issue. 

    >>(If A is spouse of B, neither A nor B can be of type IOrganization; if A is employee of B, A can be of any type descending from IParty, while B has to be of type IPerson; If A is the son of B, A has to be of type IPerson and DirectCast(A, IPerson).Gender must be Male.)

    Your above statement is more likely a specific impelement, rather than some kind of specification. So I would recommend you think about another design mode.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Etienne-Louis Tuesday, February 26, 2013 6:10 PM
    Tuesday, February 26, 2013 4:31 PM
    Moderator

All replies

  • Assuming I have the following interfaces:


    How do I have to specify the constraints in the interface IRelationshipType in order to accept only IParty or one of its descendants as type?

    Many thanks & kind regards,

    Etienne

    Hi,

    Give this a try please. I was thinking that the following top line maybe enough?

    Public Interface IRelationshipType(Of PartyType As IParty)

    '

    'You could try the following as you may have methods defined in IPerson

    'or IOrganization that are not in Iparty.

    '

    Public Interface IParty
        ' ...
    End Interface
    
    Public Interface IPerson
        Inherits IParty
        ' ...
    End Interface
    
    Public Interface IOrganization
        Inherits IParty
        ' ...
    End Interface
    
    Public Interface IRelationshipType(Of PartyType As IParty, IPerson, IOrganisation)
        ' ...
    End Interface


    Regards,

    profile for John Anthony Oliver at Stack Overflow, Q&A for professional and enthusiast programmers

    Click this link to see the NEW way of how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    App Hub for Windows Phone & XBOX 360 developers.

    Saturday, February 23, 2013 2:22 PM
  • Hi John,

    Many thanks for your quick response. Actually my first thought was to use your approach, but since IRelationshipType needs the types of the descendents of IParty rather than the interfaces itself, shouldn't the generic parameters be of type System.Type? Obviously my original question was not very detailled, I'll try to get more specific:

    IRelationshipType should represent a type of relationship, not a relationship as such (e.g parent/child, employer/employee). The implementation of IRelationsshipType should allow to set the conditions the two parties have to satisfy. (If A is spouse of B, neither A nor B can be of type IOrganization; if A is employee of B, A can be of any type descending from IParty, while B has to be of type IPerson; If A is the son of B, A has to be of type IPerson and DirectCast(A, IPerson).Gender must be Male.):

    ''' <summary>Types of relationships</summary>
    ''' <typeparam name="TPartyType1"><see cref="Type"/> the party who has a relationship with a another party. The type must be or inherit from <see cref="IParty"/></typeparam>
    ''' <typeparam name="TPartyType2"><see cref="System.Type"/> of the <see cref="IParty"/> related to <c>PartyType</c></typeparam>
    Public Interface IRelationshipType(Of TPartyType1 As Type, TPartyType2 As Type)
    
    	''' <summary><see cref="System.Type"/> of the first party. The type must <see cref="IParty"/> or a descendant.</summary>
    	Property PartyType As TPartyType1
    
    	''' <summary>Name of the relationship. </summary>
    	Property Name As String
    
    	''' <summary><see cref="System.Type"/> of the second party. The type must <see cref="IParty"/> or a descendant.</summary>
    	Property RelatedPartyType As TPartyType2
    
    	''' <summary>True if a party can have multiple relations of same type with other parties.</summary>
    	''' <remarks>e.g. A son can only have one father. A father, however, can have multiple children.</remarks>
    	Property AllowMultiple As Boolean
    
    	''' <summary>The corresponding relationship TPartyType2 -> TPartyType1</summary>
    	''' <returns>Opposite relationship type (E.g. if this Type is 'A is parent of B', the property returns the relationship type 'B is child of A'.)</returns>
    	Property OppositeRelationsshipType As IRelationshipType(Of TPartyType2, TPartyType1)
    
    
    End Interface
    

    IPartyA and IPartyB have a m:n relation, represented by IRelationship:

    ''' <summary>Relationship between two parties.</summary>
    Public Interface IRelationship
    	Property Party1 As IParty
    	Property RelationshipType As IRelationshipType(Of Type, Type)
    	Property Party2 As IParty
    End Interface

    And, finally, IParty has a property Relationsships as follows:

    ''' <summary>Base interface for individual and juristic persons.</summary>
    Public Interface IParty
    
    	Property DisplayName As String
    
    	' ...
    
    	ReadOnly Property Relationships As IList(Of IRelationship)
    
    End Interface

    Many thanks & kind regards,

    Etienne

    Sunday, February 24, 2013 7:13 AM
  • Hi Etienne,

    Welcome to the MSDN Forum.

    Based on my understanding, this is not a kind of generic contraints issue. 

    >>(If A is spouse of B, neither A nor B can be of type IOrganization; if A is employee of B, A can be of any type descending from IParty, while B has to be of type IPerson; If A is the son of B, A has to be of type IPerson and DirectCast(A, IPerson).Gender must be Male.)

    Your above statement is more likely a specific impelement, rather than some kind of specification. So I would recommend you think about another design mode.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Etienne-Louis Tuesday, February 26, 2013 6:10 PM
    Tuesday, February 26, 2013 4:31 PM
    Moderator
  • Hi Mike,

    You're absolutely right. Thank you for your input, I adopted my design accordingly.

    Kind regards,

    Etienne

    Tuesday, February 26, 2013 6:08 PM