By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,949 Members | 1,554 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,949 IT Pros & Developers. It's quick & easy.

Overloads Property Questions

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
WStoreyII,
I'm not following specifically what you are doing. It sounds like you are
doing what the 3 of the overloaded DataRow.Item properties do. You pass an
Enum that acts as the "Version" (criteria), plus a value (what the criteria
matches on).

http://msdn.microsoft.com/library/de...ItemTopic5.asp

Overloads Public Default Readonly Property Item( _
ByVal columnName As String, _
ByVal version As DataRowVersion _
) As Object

Where DataRowVersion is an Enum.
The other option instead of an Enum might be an IComparer like object or
delegate that knows what property to look for the item in. Ala VB.NET 2005's
Predicate(Of T) Delegate and Array.Find method.

The Predicate Delegate is an address of a method that "defines a set of
criteria and determines whether the specified object meets those criteria".
http://msdn2.microsoft.com/library/bfcke1bz.aspx

While Array.Find scans the array Invoking the Predicate until a match is
found.

http://msdn2.microsoft.com/library/d9hy2xwa.aspx

A variation of the above would be to have the Predicate accept both the
value to look for & the value to check. Ala the Comparison(Of T) Delegate.

http://msdn2.microsoft.com/library/tfakywbh.aspx

Note the (Of T) above indicates a Generic, in VB.NET 2003 you cannot use the
Generic, however you can simply define the Delegates using Objects...

Public Delegate Comparison(ByVal x As Object, ByVal y As Object) As
Integer

Public Delegate Predicate(ByVal x As Object) As Boolean

Hope this helps
Jay


"WStoreyII" <WS*******@discussions.microsoft.com> wrote in message
news:7C**********************************@microsof t.com...
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

Nov 21 '05 #2

P: n/a
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

Nov 21 '05 #3

P: n/a
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

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.