|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
OOP Question - Implementation, I thinkPublic Class Category Public Name As String Public SubCategory As String End Class Then, I created an array: Public Categories As Category() I created a sort method too: Public Class CategoryMethods Public Shared Sub Sort() Dim catCompare As ItemComparer = New ItemComparer() Array.Sort(Categories, catCompare) End Sub End Class Now what I want to know is this: how do I make it so that instead of just calling CategoryMethods.Sort(), how can I call Categories.Sort() and still be using my Sort method? Inherit from a class that has a sort method. A SortedList comes to mind,
although it will always be sorted. :-) -- Show quoteGregory A. Beamer MVP; MCP: +I, SE, SD, DBA *************************** Think Outside the Box! *************************** "Alex C. Barberi" wrote: > I created a class like so in VB.NET: > Public Class Category > Public Name As String > Public SubCategory As String > End Class > > Then, I created an array: > Public Categories As Category() > > I created a sort method too: > Public Class CategoryMethods > Public Shared Sub Sort() > Dim catCompare As ItemComparer = New ItemComparer() > Array.Sort(Categories, catCompare) > End Sub > End Class > > Now what I want to know is this: how do I make it so that instead of just > calling CategoryMethods.Sort(), how can I call Categories.Sort() and still be > using my Sort method? > > -- > Alex C. Barberi > Chief Executive Officer > VisionForce > http://www.visionforceweb.com > No, that didn't work. Here is what I did:
Public Class clsItem Public Name As String Public Sub DoMySort() 'sorting code here End Sub End Class Public Class clsCategory Inherits clsItem Public SubOf As String End Class Public Module mdMain Public Categories As clsCategory() Public Sub Test() Categories.DoMySort() End Sub End Module That did not work, as I knew it wouldn't. I must've misunderstood your post. Show quote "Cowboy (Gregory A. Beamer) - MVP" wrote: > Inherit from a class that has a sort method. A SortedList comes to mind, > although it will always be sorted. :-) > > -- > Gregory A. Beamer > MVP; MCP: +I, SE, SD, DBA > > *************************** > Think Outside the Box! > *************************** > > > "Alex C. Barberi" wrote: > > > I created a class like so in VB.NET: > > Public Class Category > > Public Name As String > > Public SubCategory As String > > End Class > > > > Then, I created an array: > > Public Categories As Category() > > > > I created a sort method too: > > Public Class CategoryMethods > > Public Shared Sub Sort() > > Dim catCompare As ItemComparer = New ItemComparer() > > Array.Sort(Categories, catCompare) > > End Sub > > End Class > > > > Now what I want to know is this: how do I make it so that instead of just > > calling CategoryMethods.Sort(), how can I call Categories.Sort() and still be > > using my Sort method? > > > > -- > > Alex C. Barberi > > Chief Executive Officer > > VisionForce > > http://www.visionforceweb.com > > Hi Alex,
You can do away with the CategoryMethods and ItemComparer classes. Just have your Category class implement the IComparable interface. You'll provide the details in IComparable.CompareTo. A bit like this: ------------------------------ Public Class Category Implements IComparable Public Name As String Public SubCategory As String Public Function CompareTo(ByVal obj As Object) As Integer _ Implements IComparable.CompareTo ' Test for type compatibility. If TypeOf obj Is Category Then ' Compare however is needed; this example just compares the Names. Return Me.Name.CompareTo(CType(obj, Category).Name) Else Throw New ArgumentException("Argument is not a Category", "obj") End If End Class ------------------------------ Now, you have a few options. 1) You can use an ArrayList instead of an array, which makes it easier to add and remove items to/from, and just call it's Sort method (it will detect that the items are IComparable and sort as you specified). 2) You can still use an array and just call Array.Sort(Categories) (again, it will detect IComparable). 3) If you're coding for .NET 2.0, you should use the generic List<T> class to hold your items. For example: Public Categories As List(Of Category) .... Categories.Sort() I hope this helps. Regards, Matt Alex C. Barberi wrote: Show quote > I created a class like so in VB.NET: > Public Class Category > Public Name As String > Public SubCategory As String > End Class > > Then, I created an array: > Public Categories As Category() > > I created a sort method too: > Public Class CategoryMethods > Public Shared Sub Sort() > Dim catCompare As ItemComparer = New ItemComparer() > Array.Sort(Categories, catCompare) > End Sub > End Class > > Now what I want to know is this: how do I make it so that instead of just > calling CategoryMethods.Sort(), how can I call Categories.Sort() and still be > using my Sort method? > Isn't there a way that I can put any method I want to like:
Categories.MyMethod1() Categories.MyMethod2() Is this possible? Show quote "Matt Sollars" wrote: > Hi Alex, > > You can do away with the CategoryMethods and ItemComparer classes. Just > have your Category class implement the IComparable interface. You'll > provide the details in IComparable.CompareTo. A bit like this: > > ------------------------------ > > Public Class Category > Implements IComparable > > Public Name As String > Public SubCategory As String > > Public Function CompareTo(ByVal obj As Object) As Integer _ > Implements IComparable.CompareTo > > ' Test for type compatibility. > If TypeOf obj Is Category Then > ' Compare however is needed; this example just compares the Names. > Return Me.Name.CompareTo(CType(obj, Category).Name) > Else > Throw New ArgumentException("Argument is not a Category", "obj") > End If > > End Class > > ------------------------------ > > Now, you have a few options. > > 1) You can use an ArrayList instead of an array, which makes it easier > to add and remove items to/from, and just call it's Sort method (it will > detect that the items are IComparable and sort as you specified). > > 2) You can still use an array and just call Array.Sort(Categories) > (again, it will detect IComparable). > > 3) If you're coding for .NET 2.0, you should use the generic List<T> > class to hold your items. For example: > > Public Categories As List(Of Category) > .... > Categories.Sort() > > > I hope this helps. > > > Regards, > > Matt > > > Alex C. Barberi wrote: > > I created a class like so in VB.NET: > > Public Class Category > > Public Name As String > > Public SubCategory As String > > End Class > > > > Then, I created an array: > > Public Categories As Category() > > > > I created a sort method too: > > Public Class CategoryMethods > > Public Shared Sub Sort() > > Dim catCompare As ItemComparer = New ItemComparer() > > Array.Sort(Categories, catCompare) > > End Sub > > End Class > > > > Now what I want to know is this: how do I make it so that instead of just > > calling CategoryMethods.Sort(), how can I call Categories.Sort() and still be > > using my Sort method? > > > Alex,
Absolutely! You can create your own list class to hold Category items. It should inherit from ArrayList, CollectionBase or another similar type. That way, you can add whatever methods you'd like to it. Note, if you descent from CollectionBase, you'll need to write a Sort method (just call InnerList.Sort() in your implementation) in addition to your others. Have fun. Matt Alex C. Barberi wrote: Show quote > Isn't there a way that I can put any method I want to like: > Categories.MyMethod1() > Categories.MyMethod2() > > Is this possible? > I'm not sure I understand; please explain.
I have this: Public Categories As clsCategory() And I want to be able to do this: Categories.Emulate() Categories.CreateRoster() How do I do that? Show quote "Matt Sollars" wrote: > Alex, > > Absolutely! You can create your own list class to hold Category items. > It should inherit from ArrayList, CollectionBase or another similar > type. That way, you can add whatever methods you'd like to it. > > Note, if you descent from CollectionBase, you'll need to write a Sort > method (just call InnerList.Sort() in your implementation) in addition > to your others. > > Have fun. > > Matt > > Alex C. Barberi wrote: > > Isn't there a way that I can put any method I want to like: > > Categories.MyMethod1() > > Categories.MyMethod2() > > > > Is this possible? > > > Alex,
You're still creating a simple array of clsCategory objects. What you want to do is create a replacement for the array; your own list. Declare another class. It would be something like this: ---------------------------------------- Imports System.Collections Public Class CategoryCollection Inherits CollectionBase Public Sub New() MyBase.New() End Sub Default Public ReadOnly Property Item(ByVal index As Integer) As Category Get Return CType(List(index), Category) End Get End Property Public Function Add(ByVal item As Category) As Integer Return List.Add(item) End Function Public Sub Insert(ByVal index As Integer, ByVal item As Category) List.Insert(index, item) End Sub Public Sub Remove(ByVal item As Category) List.Remove(item) End Sub Public Function IndexOf(ByVal item As Category) As Integer Return List.IndexOf(item) End Function Public Sub Sort() InnerList.Sort() End Sub Public Sub Emulate() ' Implement your emulate method. End Sub Public Sub CreateRoster() ' Implement your create roster method. End Sub End Class ---------------------------------------- Then set it all up in your program like this: Dim Category As Category Dim Categories As New CategoryCollection() ' Add some categories to the collection. MyCategory = New Category() MyCategory.Name = "Dairy" MyCategory.SubCategory = "Milk" Categories.Add(MyCategory) MyCategory = New Category() MyCategory.Name = "Canned" MyCategory.SubCategory = "Spam" Categories.Add(MyCategory) ' Call some work methods on the collection. Categories.Sort() Categories.Emulate() I hope that clears things up. Matt Alex C. Barberi wrote: Show quote > I'm not sure I understand; please explain. > > I have this: > Public Categories As clsCategory() > > And I want to be able to do this: > Categories.Emulate() > Categories.CreateRoster() > > How do I do that? > Wow! Thanks, that's great.
Show quote "Matt Sollars" wrote: > Alex, > > You're still creating a simple array of clsCategory objects. What you > want to do is create a replacement for the array; your own list. Declare > another class. It would be something like this: > > ---------------------------------------- > > Imports System.Collections > > Public Class CategoryCollection > Inherits CollectionBase > > Public Sub New() > MyBase.New() > End Sub > > Default Public ReadOnly Property Item(ByVal index As Integer) As Category > Get > Return CType(List(index), Category) > End Get > End Property > > Public Function Add(ByVal item As Category) As Integer > Return List.Add(item) > End Function > > Public Sub Insert(ByVal index As Integer, ByVal item As Category) > List.Insert(index, item) > End Sub > > Public Sub Remove(ByVal item As Category) > List.Remove(item) > End Sub > > Public Function IndexOf(ByVal item As Category) As Integer > Return List.IndexOf(item) > End Function > > Public Sub Sort() > InnerList.Sort() > End Sub > > Public Sub Emulate() > ' Implement your emulate method. > End Sub > > Public Sub CreateRoster() > ' Implement your create roster method. > End Sub > > End Class > > ---------------------------------------- > > Then set it all up in your program like this: > > Dim Category As Category > Dim Categories As New CategoryCollection() > > ' Add some categories to the collection. > MyCategory = New Category() > MyCategory.Name = "Dairy" > MyCategory.SubCategory = "Milk" > Categories.Add(MyCategory) > > MyCategory = New Category() > MyCategory.Name = "Canned" > MyCategory.SubCategory = "Spam" > Categories.Add(MyCategory) > > ' Call some work methods on the collection. > Categories.Sort() > Categories.Emulate() > > > I hope that clears things up. > > Matt > > > Alex C. Barberi wrote: > > I'm not sure I understand; please explain. > > > > I have this: > > Public Categories As clsCategory() > > > > And I want to be able to do this: > > Categories.Emulate() > > Categories.CreateRoster() > > > > How do I do that? > > > |
|||||||||||||||||||||||