Advanced OOP Question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have this:

Public Class clsItem
Implements IComparer

Public Name As String

Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
Implements IComparer.Compare
Dim empx As clsItem = DirectCast(x, clsItem)
Dim empy As clsItem = DirectCast(y, clsItem)

Return String.Compare(empx.Name, empy.Name)
End Function
End Class

<Serializable()> Public Class clsCategory
Inherits clsItem

Public SubOf As String
End Class

Public Class CategoryCollection
Inherits CollectionBase

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

Public Function Add(ByVal item As clsCategory) As Integer
Return List.Add(item)
End Function

Public Sub Insert(ByVal index As Integer, ByVal item As clsCategory)
List.Insert(index, item)
End Sub

Public Sub Remove(ByVal item As clsCategory)
List.Remove(item)
End Sub

Public Function IndexOf(ByVal item As clsCategory) As Integer
Return List.IndexOf(item)
End Function

Public Sub Sort()
InnerList.Sort(Me)
End Sub
End Class

<Serializable()> Public Class clsFile
Inherits clsItem

Public Category As String
Public FileName As String
Public Description As String
End Class

Public Class FileCollection
Inherits CollectionBase

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

Public Function Add(ByVal item As clsFile) As Integer
Return List.Add(item)
End Function

Public Sub Insert(ByVal index As Integer, ByVal item As clsFile)
List.Insert(index, item)
End Sub

Public Sub Remove(ByVal item As clsFile)
List.Remove(item)
End Sub

Public Function IndexOf(ByVal item As clsFile) As Integer
Return List.IndexOf(item)
End Function

Public Sub Sort()
InnerList.Sort(Me)
End Sub
End Class

Now isn't there some way that I can create just an ItemCollection instead of
having to create a CategoryCollection, a FileCollection, and so on for every
new class I create?
 
there are programs which are able to generate source code for a strong typed
collection given a template and a type. for example the free IDE
sharpdevelop is able to do that.
 
Alex said:
Now isn't there some way that I can create just an ItemCollection instead of
having to create a CategoryCollection, a FileCollection, and so on for every
new class I create?

Since you said you are pre-VS2005, your best option is a code
generation tool such as CodeSmith (that's actually the only one I
know). You will have to write your own templates, most likely, but it's
pretty straightforward.
 
Back
Top