473,404 Members | 2,179 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,404 software developers and data experts.

Creating a new collection from an existing collection

Hi,

I have a vb.net class collection which contains many instances of a
country class. Each country class has properties such as name, id,
currency etc.

The collection has been created without any problem. What I want to do
now is create a new subset of this collection (in a new collection)
that only contains countries that have a currency of Euro.

Anyone got any ideas how/if this can be achieved. I don't want to hit
the database again so need to create it from the existing collection
object.

Thanks,

Marcus.

Nov 21 '05 #1
5 1657
So what is the problem?
dim new_collection as new collection
for each _item in old_collection
if _item.currecy=euro then new_collection
next

<ma*********@smart-rs.com> schrieb im Newsbeitrag
news:11*********************@g47g2000cwa.googlegro ups.com...
Hi,

I have a vb.net class collection which contains many instances of a
country class. Each country class has properties such as name, id,
currency etc.

The collection has been created without any problem. What I want to do
now is create a new subset of this collection (in a new collection)
that only contains countries that have a currency of Euro.

Anyone got any ideas how/if this can be achieved. I don't want to hit
the database again so need to create it from the existing collection
object.

Thanks,

Marcus.

Nov 21 '05 #2
Hi Boni,

Thanks for that... This is how I've implemented it at the moment.
However, it seems a bit clunky to have to loop through the collection
each time and pick out the items I want. What I'm after is a way to
filter the items out of the collection in one go... much like a would
with a dataset filter, and create a new collection from that.

Any ideas?

Marcus

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Nov 21 '05 #3
The OO way to do this:

1. Create your own Collection class, CountryCollection (you may have
already done this).

2. Create an Interface:

Public Interface ICountryFilter
Function MatchesCriteria(ByVal cntr As Country) As Boolean
End Interface

3. Add a method on your collection, say:

Public Function Collection Subset(ByVal filter As ICountryFilter) As
CountryCollection

Dim matchingSubset as New CountryCollection

For each cnt As Country
If filter.MatchesCriteria(cntr) Then
matchingSubset.Add(cntr)
End If
Next

Return matchingSubset

End Function

4. Define any needed filters:

Public Class FilterByCurrency
Implements ICountryFilter

Private mDesiredCurrency as CurrencyEnum

Sub New(ByVal desiredCurrency as CurrencyEnum)
mDesiredCurrency = desiredCurrency
End Sub

Public Function MatchesCriteria(ByVal cntr As Country) As Boolean
Implements ICountryFilter.MatchesCriteria
Return (cntr.Currency = mDesiredCurrency)
End Function

End Class

5. Use them in a single line:

Dim filteredColl as CountryCollection = origCollection.Subset(new
FilterByCurrency(EuroCurrency))

Hope that helps!
Tom

PS - The above was psudoe-code and may need tweaking. Additionally,
this is overkill unless you will be filtering on other criteria.

Nov 21 '05 #4
Hi, Thnaks for your post. I'm not that familiar with Implements and
Interfaces so haven't been able to convert the psuedo code into a
working example, do you have a real code snippet you could send me
please?

It looks like this mechanism still loops through each country and
compares the currencies before adding them to the new collection? Can
you confirm?

Thanks again for your help!

Marcus

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Nov 21 '05 #5
Marcus,

Yes, the filtering is done via walking the collection.

Tom

Nov 21 '05 #6

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

Similar topics

2
by: PK | last post by:
Hello, I am looking for help on the following. I'm trying to create a custom browser toolbar button that will do a few things. One that I'm trying to do at the moment is just simply return the...
34
by: Ville Voipio | last post by:
I would need to make some high-reliability software running on Linux in an embedded system. Performance (or lack of it) is not an issue, reliability is. The piece of software is rather simple,...
44
by: Greg Strong | last post by:
Hello All, Is it better to create a query in DAO where a report has 4 sub-reports each of whose record source is a query created at runtime and everything is in 1 MDB file? From what I've...
7
by: Jon Davis | last post by:
I'm managing "collections" / "lists" (??) of objects that are internally bound by an XML document, but I do NOT intend to offer an IEnumerator interface. For instance, with a root XML document,...
10
by: aa7im | last post by:
I have a similiar thread going about this topic but I decided to break it off into a seperate discussion. Question: What is the best way to determine if an object "IsNew" to a collection? ...
3
by: Ken Varn | last post by:
I am just starting the process of creating ASP.NET server controls. I have created controls for .NET applications, but have just started with ASP.NET. I am a little confused about some areas that...
2
by: marcus.wade | last post by:
Hi, I have a vb.net class collection which contains many instances of a country class. Each country class has properties such as name, id, currency etc. The collection has been created...
5
by: John Scott | last post by:
Ok..this a rather odd question/problem. I haven't really found a straight forward answer to how to handle this scenario, so I hope someone here can help. Here it is: I have an application...
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.