dcsimg
Results 1 to 11 of 11

Thread: Inheriting a generic Class based on Generic Interface

Threaded View

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Location
    UK
    Posts
    6

    Question Inheriting a generic Class based on Generic Interface

    I want to define a generic interface which will be implemented by an abstract Generic Class. Basically this generic class is a collection class of any class.

    Interfaces are in a separate project saved as FileReconciliation.

    Here are the interface definitions
    Code:
    Imports System.Collections
    
    Public Interface ICollectionCommon(Of T As Class)
        Inherits IEnumerable
        Function Exists(ByVal oKey As Object) As Boolean
        ReadOnly Property Count() As Long
        Sub Remove(ByVal oKey As Object)
        Function Add(Optional ByVal oObject As T = Nothing, Optional ByVal sKey As String = "") As T
        Default ReadOnly Property Item(ByVal oKey As Object) As T
    End Interface
    
    Public Interface ITag
        Property Tag() As String
        Property TagValue() As String
    End Interface
    
    Public Interface ITransaction
        Property Tags() As ICollectionCommon(Of ITag)
    End Interface
    
    Public Interface IFileInformation
        ReadOnly Property FileName() As String
        ReadOnly Property FileType() As FileType
        ReadOnly Property BICForwardingBank() As String
        ReadOnly Property BICExecutingBank() As String
        ReadOnly Property GetFileLevelTags() As ICollectionCommon(Of ITag)
        ReadOnly Property TimeStamp() As String
        ReadOnly Property Transactions() As ICollectionCommon(Of ITransaction)
        Sub GetFileInfo(ByVal sFileName As String)
    End Interface
    
    Public Enum FileType
        frFileTypeUnknown
        frFileTypeDriectDebit
        frFileTypeDirectCredit
    End Enum
    Here is the code for CollectionClass which implements ICollectionCommon
    Code:
    Imports FileReconciliation
    
    Public MustInherit Class CollectionClass(Of AnyObject As {Class, New})
        Implements ICollectionCommon(Of AnyObject)
    
        Private clnCollectionOfOjects As Collection
    
        Public Function Add(Optional ByVal oAnyObject As AnyObject = Nothing, Optional ByVal sKey As String = "") As AnyObject Implements ICollectionCommon(Of AnyObject).Add
            Dim objNewMember As AnyObject
            If oAnyObject Is Nothing Then
                objNewMember = New AnyObject
            Else
                objNewMember = oAnyObject
            End If
            If sKey.Trim().Length = 0 Then
                clnCollectionOfOjects.Add(objNewMember)
            Else
                clnCollectionOfOjects.Add(objNewMember, sKey)
            End If
            Return objNewMember
        End Function
    
        Public ReadOnly Property Count() As Long Implements ICollectionCommon(Of AnyObject).Count
            Get
                Count = clnCollectionOfOjects.Count
            End Get
        End Property
    
        Public Function Exists(ByVal oKey As Object) As Boolean Implements ICollectionCommon(Of AnyObject).Exists
            If clnCollectionOfOjects(oKey) Is Nothing Then
                Return False
            Else
                Return True
            End If
        End Function
    
        Default Public ReadOnly Property Item(ByVal oKey As Object) As AnyObject Implements ICollectionCommon(Of AnyObject).Item
            Get
                Item = clnCollectionOfOjects(oKey)
            End Get
        End Property
    
        Public Sub Remove(ByVal oKey As Object) Implements ICollectionCommon(Of AnyObject).Remove
            clnCollectionOfOjects.Remove(oKey)
        End Sub
    
        Public Function GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
            GetEnumerator = clnCollectionOfOjects.GetEnumerator
        End Function
    
        Public Sub New()
            clnCollectionOfOjects = New Collection
        End Sub
    
        Protected Overrides Sub Finalize()
            clnCollectionOfOjects = Nothing
        End Sub
    End Class
    Other Classes based on other interfaces are as follows
    Code:
    Imports FileReconciliation
    
    Public Class clsFileInformation1
        Implements IFileInformation
    
        Dim strFileName As String
        Dim intFileType As FileType
        Dim strBICForwardingBank As String
        Dim strBICExecutingBank As String
        Dim strTimeStamp As String
        Dim objFileLevelTags As clsTags
        Dim objTransactions As clsTransactions
    
        Public Sub New()
            objFileLevelTags = New clsTags()
            objTransactions = New clsTransactions()
        End Sub
    
        Protected Overrides Sub Finalize()
            objFileLevelTags = Nothing
            objTransactions = Nothing
            MyBase.Finalize()
        End Sub
    
        Public ReadOnly Property BICExecutingBank() As String Implements FileReconciliation.IFileInformation.BICExecutingBank
            Get
                BICExecutingBank = strBICExecutingBank
            End Get
        End Property
    
        Public ReadOnly Property BICForwardingBank() As String Implements FileReconciliation.IFileInformation.BICForwardingBank
            Get
                BICForwardingBank = strBICForwardingBank
            End Get
        End Property
    
        Public ReadOnly Property FileName() As String Implements FileReconciliation.IFileInformation.FileName
            Get
                FileName = strFileName
            End Get
        End Property
    
        Public ReadOnly Property FileType() As FileReconciliation.FileType Implements FileReconciliation.IFileInformation.FileType
            Get
                FileType = intFileType
            End Get
        End Property
    
        Public Sub GetFileInfo(ByVal sFileName As String) Implements FileReconciliation.IFileInformation.GetFileInfo
            Dim objTransaction As clsTransaction
            Dim objTag As clsTag
            Dim objTags As clsTags
            strFileName = sFileName
            objTags = New clsTags()
            objTag = objTags.Add()
            objTag.Tag = ":20:"
            objTag.TagValue = ":20: Value"
            objTag = objTags.Add(sKey:=":30:")
            objTag.Tag = ":30:"
            objTag.TagValue = ":30: Value"
            objTransaction = objTransactions.Add()
            objTransaction.Tags = objTags  'exception is thrown here
            objFileLevelTags = objTags
        End Sub
    
        Public ReadOnly Property GetFileLevelTags() As FileReconciliation.ICollectionCommon(Of FileReconciliation.ITag) Implements FileReconciliation.IFileInformation.GetFileLevelTags
            Get
                GetFileLevelTags = objFileLevelTags
            End Get
        End Property
    
        Public ReadOnly Property TimeStamp() As String Implements FileReconciliation.IFileInformation.TimeStamp
            Get
                TimeStamp = strTimeStamp
            End Get
        End Property
    
        Public ReadOnly Property Transactions() As FileReconciliation.ICollectionCommon(Of FileReconciliation.ITransaction) Implements FileReconciliation.IFileInformation.Transactions
            Get
                Transactions = objTransactions
            End Get
        End Property
    End Class
    
    Public Class clsFilesInformation
        Inherits CollectionClass(Of clsFileInformation1)
    
        Public Sub New()
            MyBase.New()
        End Sub
    End Class
    
    Public Class clsTag
        Implements ITag
    
        Private strTagID As String
        Private strTagValue As String
    
        Public Sub New()
            strTagID = ""
            strTagValue = ""
        End Sub
    
        Friend Sub New(ByVal sTagID As String)
            strTagID = sTagID
            strTagValue = ""
        End Sub
    
        Friend Sub New(ByVal sTagID As String, ByVal sTagValue As String)
            strTagID = sTagID
            strTagValue = sTagValue
        End Sub
    
        Public Property Tag() As String Implements ITag.Tag
            Get
                Tag = strTagID
            End Get
            Friend Set(ByVal value As String)
                strTagID = value
            End Set
        End Property
    
        Public Property TagValue() As String Implements ITag.TagValue
            Get
                TagValue = strTagValue
            End Get
            Friend Set(ByVal value As String)
                strTagValue = value
            End Set
        End Property
    End Class
    
    Public Class clsTags
        Inherits CollectionClass(Of clsTag)
    
        Public Sub New()
            MyBase.New()
        End Sub
    End Class
    
    Public Class clsTransaction
        Implements ITransaction
        Dim objTags As clsTags
    
        Public Property Tags() As ICollectionCommon(Of ITag) Implements ITransaction.Tags
            Get
                Tags = objTags
            End Get
            Set(ByVal value As ICollectionCommon(Of ITag))
                objTags = value
            End Set
        End Property
    End Class
    
    Public Class clsTransactions
        Inherits CollectionClass(Of clsTransaction)
    
        Public Sub New()
            MyBase.New()
        End Sub
    End Class
    Sub Main is follows in a separate module

    Imports FileReconciliation

    Code:
    Module StartUP
        <STAThread()> _
        Sub Main()
            Dim objTags As clsTags
            Dim objFilesInformation As clsFilesInformation
            Dim objFileInformation As clsFileInformation1
            objFilesInformation = New clsFilesInformation
            objFileInformation = objFilesInformation.Add()
            objFileInformation.GetFileInfo("SomeFile")
            objTags = objFileInformation.GetFileLevelTags
            objFileInformation = Nothing
            objFilesInformation = Nothing
        End Sub
    End Module

    Line of code, in second last code section in Sub GetFileInfo, marked as Bold is where an error occurs.
    Can somebody point out what I'm doing wrong?

    GenericInterfaces.zip
    Last edited by simrim1; Feb 13th, 2018 at 11:46 AM.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width