Results 1 to 3 of 3

Thread: Enforce interface implementation on propperty

  1. #1

    Thread Starter
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    Enforce interface implementation on propperty

    Hi All,

    I'm in a situation I need to Expose an object.
    the object I need to expose in a namespace I'm not allowed to reference directly as the application is modular and the module can be loaded or not.

    In a class I've got this Propperty and function:
    Code:
    Public Property ObjectToExpose As Object Implements IExposedObject.ObjectToExpose
                Get
                    Return _ObjectToExpose
                End Get
                Set(value As Object)
                    If value IsNot Nothing Then
                        If HasInterfaces(value) Then
                            _ObjectToExpose = value
                            _id = value.id
                        Else
                            Throw New Exception("Item doesn't support Idirty or IselectionListData")
                        End If
                    End If
                End Set
            End Property
    
            Private Function HasInterfaces(ByVal Item As Object) As Boolean
    
                Dim SelctionInterface As String = "ISelectionListData"
                Dim DirtyInterface As String = "IDirty"
                Dim result As Boolean
    
                If Item.GetType.GetInterface(DirtyInterface) IsNot Nothing Then
                    result = (Item.GetType.GetInterface(SelctionInterface) IsNot Nothing)
                End If
    
                Return result
    
            End Function
    How can I enforce that the object has these interfaces in this line?
    Public Property ObjectToExpose As Object
    Something like:
    Public Property (T as {"Idrty","ISelectionListData"}) ObjectToExpose As Object
    why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
    for every question you ask provide an answer on another thread.

  2. #2
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,902

    Re: Enforce interface implementation on propperty

    If the interface defines that property then anything that implements the interface must implement the property. So if your object implements IExposedObject it must, in turn implement the ObjectToExpose property (assuming that's a property on the interface). You don't need to do anything. I have a feeling I might be miss-understanding the question though.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  3. #3

    Thread Starter
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    Re: Enforce interface implementation on propperty

    Hi FunkyDexter,

    No, IExposedObject is the interface that's implemented. But you did set me on the right way for an escape route


    Code:
    Public Property ObjectToExpose As IObjecttoExpose Implements IExposedObject.ObjectToExpose
    
    Interface IObjectToExpose
    Inherits Idirty, IselectionListdata
    end interface
    The disadvantage is that I need to rewrite every class I want to expose to implement IObjecttoExpose instead of Idirty and ISelectionlistData.
    why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
    for every question you ask provide an answer on another thread.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width