Name/Value class that I can inherit?

Is there a base class that I should inherit from that is just a basic name/value pair?

For example, I have several classes that only have two properties (name and value). They all called something unique liked Field, Data, etc. and are all used for different things but the innards are all the same, two properties (name and value) and they have a related collection class that inherits from CollectionBase. Is there something I can do so they all share the same code or inherit from the same base class so it's not so redundant? They are also located in different protection levels of the assembly as well so I don't want to just use the same class for every scenario.

    Public Class Field

        Public Sub New()

        End Sub

        Private _name As String
        Public Property Name() As String
            Get
                Return _name
            End Get
            Set(ByVal value As String)
                _name = value
            End Set
        End Property

        Private _value As String
        Public Property Value() As String
            Get
                Return _value
            End Get
            Set(ByVal value As String)
                _value = value
            End Set
        End Property

    End Class


    Private NotInheritable Class FieldCollection
        Inherits CollectionBase

        Public Sub New()

        End Sub

        Public Sub Add(ByVal field As Field)
            List.Add(field)
        End Sub

        Public Sub Remove(ByVal index As Integer)
            If index > Count - 1 Or index < 0 Then
                Console.WriteLine("Can't remove this item")
            Else
                List.RemoveAt(index)
            End If
        End Sub

        Default Public ReadOnly Property Item(ByVal index As Integer) As Field
            Get
                Return CType(List.Item(index), Field)
            End Get
        End Property

    End Class

    Public Class Data

        Public Sub New()

        End Sub

        Private _name As String
        Public Property Name() As String
            Get
                Return _name
            End Get
            Set(ByVal value As String)
                _name = value
            End Set
        End Property

        Private _value As String
        Public Property Value() As String
            Get
                Return _value
            End Get
            Set(ByVal value As String)
                _value = value
            End Set
        End Property

    End Class


    Private NotInheritable Class DataCollection
        Inherits CollectionBase

        Public Sub New()

        End Sub

        Public Sub Add(ByVal data As Data)
            List.Add(data)
        End Sub

        Public Sub Remove(ByVal index As Integer)
            If index > Count - 1 Or index < 0 Then
                Console.WriteLine("Can't remove this item")
            Else
                List.RemoveAt(index)
            End If
        End Sub

        Default Public ReadOnly Property Item(ByVal index As Integer) As Data
            Get
                Return CType(List.Item(index), Data)
            End Get
        End Property

    End Class

etc...

UPDATE

Here's a PasteBin of the code that I'm looking to streamline. Hopefully this helps.

Answers


See KeyValuePair<TKey, TValue>. This structure is used for example by .NET's Dictionary<TKey, TValue> class to store generic values against a specific key and by the sounds of it should suit your requirements as long as inheritance is not an issue.

If however you do want to use inheritance of any kind, then you can either use .NET4s Tuple<T1, T2> (Although this would result in .Item1, .Item2) or alternatively, if inheritance is really required / needed, use composition to wrap the KeyValuePair<TKey, TValue> into a custom object.

Edit: Seeing your example this looks like a good use case for a Dictionary<string, string>. Is there a specific reason why you do not want to use a Dictionary or any of the other build in collection clases which'd be quite suitable for this? You can always "protect" certain classes by declaring them or their methods internal, private, protected etc to avoid them leaking too deep down, or too far up, your object model.


Need Your Help

Using same set of coding in different view controller

ios objective-c

I have a same set of coding that have to be used in different view controller.what I have to do, to avoid duplication of coding in every view controller.I couldn't find the exact solution in google...

Is there a way to automatically determine dependencies when setting up a dpkg control file?

linux ubuntu package debian dpkg

Most of the fields in a dpkg (Debian) control file are straightforward. The tricky one is determining the list of dependencies (Depends:). I was hoping that dpkg-gencontrol could do this for me by

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.