473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Generic List Find.

How do I write the following c# code in vb

Product FindProduct(string code)
{
List<Productproducts = getProducts();
return products.Find(delegate(Product bo)
{ return bo.Code == code; });
}
Oct 18 '07 #1
9 7566
On Oct 18, 2:56 pm, Satish Itty <sittyNOS...@clayton.comwrote:
How do I write the following c# code in vb

Product FindProduct(string code)
{
List<Productproducts = getProducts();
return products.Find(delegate(Product bo)
{ return bo.Code == code; });

}- Hide quoted text -

- Show quoted text -
Function FindProduct (ByVal code As String) As Product
Dim products As List(Of Product) = GetProducts()
Dim pred As New PredicateClass(code)

Return products.Find (AddressOf pred.PredicateFunction)
End Function

Private Class PredicateClass
Private code As String
Public Sub New (ByVal code As String)
Me.code = code
End Sub

Public Function PredicateFunction (ByVal bo As Product) As Boolean
Return bo.Code = Me.code
End Function
End Class

Anyway, that should be a good approximation (I just did that off the
top of my head, so it's not tested, and may actually contain some
stray C# syntax :)... Man am I grateful for anonymous methods in C# :)

--
Tom Shelton

Oct 18 '07 #2
Thanks Tom I wish they had anonymous methods in VB as well.

Tom Shelton wrote:
On Oct 18, 2:56 pm, Satish Itty <sittyNOS...@clayton.comwrote:
>How do I write the following c# code in vb

Product FindProduct(string code)
{
List<Productproducts = getProducts();
return products.Find(delegate(Product bo)
{ return bo.Code == code; });

}- Hide quoted text -

- Show quoted text -

Function FindProduct (ByVal code As String) As Product
Dim products As List(Of Product) = GetProducts()
Dim pred As New PredicateClass(code)

Return products.Find (AddressOf pred.PredicateFunction)
End Function

Private Class PredicateClass
Private code As String
Public Sub New (ByVal code As String)
Me.code = code
End Sub

Public Function PredicateFunction (ByVal bo As Product) As Boolean
Return bo.Code = Me.code
End Function
End Class

Anyway, that should be a good approximation (I just did that off the
top of my head, so it's not tested, and may actually contain some
stray C# syntax :)... Man am I grateful for anonymous methods in C# :)

--
Tom Shelton
Oct 19 '07 #3
Why not just code it the straight forward way?

Function FindProduct(code as string) as Product
dim products as List(of Product) = GetProduct()
for each p as product in Products
if p.code = code then
return p
end if
return nothing
end function
--
Terry
"Satish Itty" wrote:
How do I write the following c# code in vb

Product FindProduct(string code)
{
List<Productproducts = getProducts();
return products.Find(delegate(Product bo)
{ return bo.Code == code; });
}
Oct 19 '07 #4

"Terry" <Te****@nospam.nospamwrote in message
news:00**********************************@microsof t.com...
Why not just code it the straight forward way?

Function FindProduct(code as string) as Product
dim products as List(of Product) = GetProduct()
for each p as product in Products
if p.code = code then
return p
end if
return nothing
end function
--
Terry
"Satish Itty" wrote:
>How do I write the following c# code in vb

Product FindProduct(string code)
{
List<Productproducts = getProducts();
return products.Find(delegate(Product bo)
{ return bo.Code == code; });
}
Can you change or overload the GetProduct to take a code as a parameter.
Seems like alot of extra processing to retrieve all the info and then filter
it.

LS

Oct 19 '07 #5
I had assumed in this example that GetProducts() was just going to return a
reference to an existing List. The real point I was making here, was that,
while both Satish and Tom were lamenting the lack of 'anonymous methods ' in
VB, why would you use one in this case. And I think that holds true for both
C# and VB. That is unless 'Find' has some built in speed improvemnt that
will make up for a totally un-needed function call.
--
Terry
"Lloyd Sheen" wrote:
>
"Terry" <Te****@nospam.nospamwrote in message
news:00**********************************@microsof t.com...
Why not just code it the straight forward way?

Function FindProduct(code as string) as Product
dim products as List(of Product) = GetProduct()
for each p as product in Products
if p.code = code then
return p
end if
return nothing
end function
--
Terry
"Satish Itty" wrote:
How do I write the following c# code in vb

Product FindProduct(string code)
{
List<Productproducts = getProducts();
return products.Find(delegate(Product bo)
{ return bo.Code == code; });
}

Can you change or overload the GetProduct to take a code as a parameter.
Seems like alot of extra processing to retrieve all the info and then filter
it.

LS

Oct 19 '07 #6
Well, I stand corrected.... Find is faster then using Enumeration, even when
you have to go through the contortions that Tom suggested. The results for
the test code that follows were:

Starting test.....
Elasped time for Enumeration is 00:00:13.0926257
Elasped time for Find is 00:00:12.4814362

and the test I used....

Module Module1
Dim Products As New List(Of Product)
Dim TestCount As Integer = 10000

Sub Main()
Dim I As Integer
For I = 1 To TestCount
Dim p As New Product(I)
Products.Add(p)
Next
Dim sw As New System.Diagnostics.Stopwatch
Console.WriteLine("Starting test.....")
sw.Start()
For I = 1 To TestCount
FindProduct(I.ToString)
Next
sw.Stop()
Console.WriteLine("Elasped time for Enumeration is {0}", sw.Elapsed)
sw.Reset()
sw.Start()
For I = 1 To TestCount
FindProduct2(I.ToString)
Next
sw.Stop()
Console.WriteLine("Elasped time for Find is {0}", sw.Elapsed)
Console.ReadLine()
End Sub

Private Function FindProduct(ByVal code As String) As Product
For Each p As Product In Products
If p.Code = code Then Return p
Next
Return Nothing
End Function

Function FindProduct2(ByVal code As String) As Product
Dim pred As New PredicateClass(code)

Return products.Find(AddressOf pred.PredicateFunction)
End Function

Public Class Product
Private _Code As String
Public ReadOnly Property Code() As String
Get
Return _Code
End Get
End Property

Public Sub New(ByVal c As Integer)
_Code = c.ToString
End Sub
End Class

Private Class PredicateClass
Private code As String
Public Sub New(ByVal code As String)
Me.code = code
End Sub

Public Function PredicateFunction(ByVal bo As Product) As Boolean
Return bo.Code = Me.code
End Function
End Class

End Module
--
Terry
"Terry" wrote:
I had assumed in this example that GetProducts() was just going to return a
reference to an existing List. The real point I was making here, was that,
while both Satish and Tom were lamenting the lack of 'anonymous methods ' in
VB, why would you use one in this case. And I think that holds true for both
C# and VB. That is unless 'Find' has some built in speed improvemnt that
will make up for a totally un-needed function call.
--
Terry
"Lloyd Sheen" wrote:

"Terry" <Te****@nospam.nospamwrote in message
news:00**********************************@microsof t.com...
Why not just code it the straight forward way?
>
Function FindProduct(code as string) as Product
dim products as List(of Product) = GetProduct()
for each p as product in Products
if p.code = code then
return p
end if
return nothing
end function
>
>
--
Terry
>
>
"Satish Itty" wrote:
>
>How do I write the following c# code in vb
>>
>Product FindProduct(string code)
>{
>List<Productproducts = getProducts();
>return products.Find(delegate(Product bo)
>{ return bo.Code == code; });
>}
>>
Can you change or overload the GetProduct to take a code as a parameter.
Seems like alot of extra processing to retrieve all the info and then filter
it.

LS
Oct 19 '07 #7
Thanks terry for comparing both options.
Lloyd The reason for my approach was because the products list is cached
and all getProducts() does it to get a list from the cache. So whether I
overload getProducts() to take the parameter or not I will have to do
this process somewhere.
So I guess I will go with toms suggestion of predicateclass
Terry wrote:
Well, I stand corrected.... Find is faster then using Enumeration, even when
you have to go through the contortions that Tom suggested. The results for
the test code that follows were:

Starting test.....
Elasped time for Enumeration is 00:00:13.0926257
Elasped time for Find is 00:00:12.4814362

and the test I used....

Module Module1
Dim Products As New List(Of Product)
Dim TestCount As Integer = 10000

Sub Main()
Dim I As Integer
For I = 1 To TestCount
Dim p As New Product(I)
Products.Add(p)
Next
Dim sw As New System.Diagnostics.Stopwatch
Console.WriteLine("Starting test.....")
sw.Start()
For I = 1 To TestCount
FindProduct(I.ToString)
Next
sw.Stop()
Console.WriteLine("Elasped time for Enumeration is {0}", sw.Elapsed)
sw.Reset()
sw.Start()
For I = 1 To TestCount
FindProduct2(I.ToString)
Next
sw.Stop()
Console.WriteLine("Elasped time for Find is {0}", sw.Elapsed)
Console.ReadLine()
End Sub

Private Function FindProduct(ByVal code As String) As Product
For Each p As Product In Products
If p.Code = code Then Return p
Next
Return Nothing
End Function

Function FindProduct2(ByVal code As String) As Product
Dim pred As New PredicateClass(code)

Return products.Find(AddressOf pred.PredicateFunction)
End Function

Public Class Product
Private _Code As String
Public ReadOnly Property Code() As String
Get
Return _Code
End Get
End Property

Public Sub New(ByVal c As Integer)
_Code = c.ToString
End Sub
End Class

Private Class PredicateClass
Private code As String
Public Sub New(ByVal code As String)
Me.code = code
End Sub

Public Function PredicateFunction(ByVal bo As Product) As Boolean
Return bo.Code = Me.code
End Function
End Class

End Module
Oct 19 '07 #8
Thanks terry for comparing both options.
Lloyd The reason for my approach was because the products list is cached
and all getProducts() does it to get a list from the cache. So whether I
overload getProducts() to take the parameter or not I will have to do
this process somewhere.
So I guess I will go with toms suggestion of predicateclass

Terry wrote:
Well, I stand corrected.... Find is faster then using Enumeration, even when
you have to go through the contortions that Tom suggested. The results for
the test code that follows were:

Starting test.....
Elasped time for Enumeration is 00:00:13.0926257
Elasped time for Find is 00:00:12.4814362

and the test I used....

Module Module1
Dim Products As New List(Of Product)
Dim TestCount As Integer = 10000

Sub Main()
Dim I As Integer
For I = 1 To TestCount
Dim p As New Product(I)
Products.Add(p)
Next
Dim sw As New System.Diagnostics.Stopwatch
Console.WriteLine("Starting test.....")
sw.Start()
For I = 1 To TestCount
FindProduct(I.ToString)
Next
sw.Stop()
Console.WriteLine("Elasped time for Enumeration is {0}", sw.Elapsed)
sw.Reset()
sw.Start()
For I = 1 To TestCount
FindProduct2(I.ToString)
Next
sw.Stop()
Console.WriteLine("Elasped time for Find is {0}", sw.Elapsed)
Console.ReadLine()
End Sub

Private Function FindProduct(ByVal code As String) As Product
For Each p As Product In Products
If p.Code = code Then Return p
Next
Return Nothing
End Function

Function FindProduct2(ByVal code As String) As Product
Dim pred As New PredicateClass(code)

Return products.Find(AddressOf pred.PredicateFunction)
End Function

Public Class Product
Private _Code As String
Public ReadOnly Property Code() As String
Get
Return _Code
End Get
End Property

Public Sub New(ByVal c As Integer)
_Code = c.ToString
End Sub
End Class

Private Class PredicateClass
Private code As String
Public Sub New(ByVal code As String)
Me.code = code
End Sub

Public Function PredicateFunction(ByVal bo As Product) As Boolean
Return bo.Code = Me.code
End Function
End Class

End Module
Oct 19 '07 #9
On Oct 19, 1:10 pm, Terry <Ter...@nospam.nospamwrote:
Well, I stand corrected.... Find is faster then using Enumeration, even when
you have to go through the contortions that Tom suggested. The results for
the test code that follows were:

Starting test.....
Elasped time for Enumeration is 00:00:13.0926257
Elasped time for Find is 00:00:12.4814362

and the test I used....

Module Module1
Dim Products As New List(Of Product)
Dim TestCount As Integer = 10000

Sub Main()
Dim I As Integer
For I = 1 To TestCount
Dim p As New Product(I)
Products.Add(p)
Next
Dim sw As New System.Diagnostics.Stopwatch
Console.WriteLine("Starting test.....")
sw.Start()
For I = 1 To TestCount
FindProduct(I.ToString)
Next
sw.Stop()
Console.WriteLine("Elasped time for Enumeration is {0}", sw.Elapsed)
sw.Reset()
sw.Start()
For I = 1 To TestCount
FindProduct2(I.ToString)
Next
sw.Stop()
Console.WriteLine("Elasped time for Find is {0}", sw.Elapsed)
Console.ReadLine()
End Sub

Private Function FindProduct(ByVal code As String) As Product
For Each p As Product In Products
If p.Code = code Then Return p
Next
Return Nothing
End Function

Function FindProduct2(ByVal code As String) As Product
Dim pred As New PredicateClass(code)

Return products.Find(AddressOf pred.PredicateFunction)
End Function

Public Class Product
Private _Code As String
Public ReadOnly Property Code() As String
Get
Return _Code
End Get
End Property

Public Sub New(ByVal c As Integer)
_Code = c.ToString
End Sub
End Class

Private Class PredicateClass
Private code As String
Public Sub New(ByVal code As String)
Me.code = code
End Sub

Public Function PredicateFunction(ByVal bo As Product) As Boolean
Return bo.Code = Me.code
End Function
End Class

End Module
--
Terry

"Terry" wrote:
I had assumed in this example that GetProducts() was just going to return a
reference to an existing List. The real point I was making here, was that,
while both Satish and Tom were lamenting the lack of 'anonymous methods ' in
VB, why would you use one in this case. And I think that holds true for both
C# and VB. That is unless 'Find' has some built in speed improvemnt that
will make up for a totally un-needed function call.
--
Terry
"Lloyd Sheen" wrote:
"Terry" <Ter...@nospam.nospamwrote in message
>news:00**********************************@microso ft.com...
Why not just code it the straight forward way?
Function FindProduct(code as string) as Product
dim products as List(of Product) = GetProduct()
for each p as product in Products
if p.code = code then
return p
end if
return nothing
end function
--
Terry
"Satish Itty" wrote:
How do I write the following c# code in vb
Product FindProduct(string code)
{
List<Productproducts = getProducts();
return products.Find(delegate(Product bo)
{ return bo.Code == code; });
}
Can you change or overload the GetProduct to take a code as a parameter.
Seems like alot of extra processing to retrieve all the info and then filter
it.
LS- Hide quoted text -

- Show quoted text -
Terry, thanks for the compare! I have never thought of it in terms of
performance, but asthetics :) But based on your test (which sparked a
little debate here :), we had a look at the implementation of
Find.... It turns out the difference is that find using for instead
of foreach to iterate the loop. For is faster then for each...

--
Tom Shelton

Oct 19 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Isaac | last post by:
Hello, I'm using the Generic List class in System.Collection.Generics to implement a strongly-typed list of my own class. I want to implement the Find methods and have to use the Predicate...
15
by: Stig Brautaset | last post by:
Hi group, I'm playing with a little generic linked list/stack library, and have a little problem with the interface of the pop() function. If I used a struct like this it would be simple: ...
3
by: Abhi | last post by:
In the following hypothetical example I want to build a generic list of unique string items. How should I implement the pred function so that it returns true/false if target string exists in the...
15
by: Brett Romero | last post by:
I have this class: public class MyClass<TValue> where TValue : IMyClass, new( ) {...} I need to create a class that will hold instances of the above class using List<>: public class...
7
by: Sehboo | last post by:
We have several generic List objects in our project. Some of them have about 1000 items in them. Everytime we have to find something, we have to do a for loop. There is one method which does the...
3
by: shapper | last post by:
Hello, I have a generic list named Rows: Dim a As New Generic.List(Of Row) Row is a structure which has 2 properties: Name and Content. Is it possible, without a for loop, to find a Row...
3
by: Allan Bredahl | last post by:
Hi All I'm trying to use a Generic.List declared as follows (Example): Public Class MyObj { Public String Name; Public Integer Age; }
1
by: shapper | last post by:
Hello, I have an Enum and a Generic.List(Of Enum) 1 Public Enum Mode 2 Count 3 Day 4 Month 5 End Enum
10
by: Egghead | last post by:
Hi all, Can someone kindly enough point me to some situations that we shall or "must" use Generic Class? I can foresee the Generic Method is powerful, but I can not find a single situation that...
2
by: SimonDotException | last post by:
I am trying to use reflection in a property of a base type to inspect the properties of an instance of a type which is derived from that base type, when the properties can themselves be instances of...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.