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.
See
KeyValuePair<TKey, TValue>. This structure is used for example by .NET’sDictionary<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 theKeyValuePair<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.