WStorey II,
What you did is fine, I gave an example of where the framework does the same
thing in some cases, the DataRow.Item property.
I was offering an alternative using Delegates, which is more work, but
significantly more flexibility.
Something like:
Option Strict On
Option Explicit On
Public Class WStoryII
Public Delegate Function Predicate(ByVal item As Customer, ByVal value
As Object) As Boolean
Public Class Customer
Public ReadOnly DateOfSale As Date
Public ReadOnly Item As String
Public ReadOnly Code As String
Public ReadOnly Name As String
Public Sub New(ByVal dateOfSale As Date, ByVal item As String, ByVal
code As String, ByVal name As String)
Me.DateOfSale = dateOfSale
Me.Item = item
Me.Code = code
Me.Name = name
End Sub
Public Overrides Function ToString() As String
Return String.Format("Customer({0:d}, {1}, {2}, {3})",
DateOfSale, Item, Code, Name)
End Function
#Region " Predefined predicates "
Public Shared Function CompareName(ByVal item As Customer, ByVal
value As Object) As Boolean
Return item.Name = CStr(value)
End Function
Public Shared Function CompareDate(ByVal item As Customer, ByVal
value As Object) As Boolean
Return item.DateOfSale = CDate(value)
End Function
Public Shared Function CompareTodayForName(ByVal item As Customer,
ByVal value As Object) As Boolean
Return item.DateOfSale.Date = DateTime.Today AndAlso item.Name =
CStr(value)
End Function
#End Region
End Class
Public Class CustomerCollection
Inherits CollectionBase
Public Sub Add(ByVal dateOfSale As Date, ByVal item As String, ByVal
code As String, ByVal name As String)
Me.InnerList.Add(New Customer(dateOfSale, item, code, name))
End Sub
Public Function Find(ByVal value As Object, ByVal predicate As
Predicate) As Customer
For Each item As Customer In Me.InnerList
If predicate.Invoke(item, value) Then
Return item
End If
Next
Return Nothing
End Function
End Class
Public Shared Sub Main()
Dim customers As New CustomerCollection
customers.Add(DateTime.Today, "A1", "A", "Jay")
customers.Add(DateTime.Today.AddDays(-1), "B1", "B", "WStoryII")
customers.Add(DateTime.Today.AddDays(1), "C1", "C", "Sam")
Dim item As Customer
item = customers.Find("WStoryII", AddressOf Customer.CompareName)
Debug.WriteLine(item, "find name")
item = customers.Find(DateTime.Now, AddressOf Customer.CompareDate)
Debug.WriteLine(item, "find now")
item = customers.Find(DateTime.Today, AddressOf
Customer.CompareDate)
Debug.WriteLine(item, "find today")
End Sub
End Class
The advantage of using Delegates you can add functions that do comparisons
without modifying the Enum or the Find itself.
I used Find instead of Item, as I am searching for the item, rather then
indexing per se, you can just as easily made Find a Default Readonly
Property.
The CompareTodayForName is an example of a more complex predicate...
Hope this helps
Jay
"WStoreyII" <WS*******@discussions.microsoft.com> wrote in message
news:45**********************************@microsof t.com...
Jay,
You have lost me, let me retry to explain what i am doing.
I have a Collection Class Called Customers
this class has a few different fields that i would use as lookup fields
for
the customer objects in its collection
List:
DateOfSale:Date
Item:String
Code:String
Name:String
Ect.
The Problem is that i Can not use overloaded Propertys Items because i
cant
not have overloaded values of the same type and i have at least three
strings
so what i did was created a enum that contains these values and then in
the
item function they will pass the criteria and then the value that is to be
matched as an object.
I just wanted to know if there was a more efficent way of doing this.
WStorey II
Thanks Again.
"WStoreyII" wrote:
I have a Collection Class That I am Creating ,
In This Collection Class I have a default Property Item.
My Problem is this I have about twenty different Criterias that can be
used
as a value in searching for this item.
So i figured that i could use overloaded item property's, however it
seems that i may not have an overloaded property of the same value type
so what i have done is i made an enum that contains all of the different
criterias and then took in as variables for the property the criteria and
the
value as an object
With the exception of the fact that the value variables can be any object
and so is
not explicitly stated, this seems to work fine.
My Question is , does anyone see any problems with this method that i
have
mentioned that i have not noticed.
and also Does any one else no of a better way of solving this little
dilema?
Thanks Again,
Really you guys are very Helpful In here!!!!
WStoreyII