473,387 Members | 1,431 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,387 software developers and data experts.

Finding type of objects in a collection

Hi all,

I'm trying to create a subclass of an ArrayList that is intended
specifically for holding FileInfo objects. My idea to restrict this is to
override the Add and AddRange methods to allow only FileInfo objects. In
AddRange, how can I verify that the collection passed is a collection of
FileInfo objects?

TIA,

John
Nov 22 '05 #1
2 1396
It is generally recommended to derive from CollectionBase instead of
ArrayList for your custom collections. Check out this article for some
hints:
http://www.c-sharpcorner.com/Code/20...ollections.asp

You can then create your own AddRange method that only accepts a
FileInfo array:

public void AddRange(FileInfo[] files)

If you want to stick with overriding the ArrayList, I don't think there
is anything you can do aside from looping over the collection and
checking the type of each object in the collection. It probably won't
be the most performant code, and you probalby lose the performance
benefit of the AddRange method.

public override void AddRange(System.Collections.ICollection c) {
foreach(object item in c){
if (!item is System.IO.FileInfo) {
throw new ArgumentException("");
}
base.AddRange(c);
}

Instead of throwing an exception if one item is bad, you could just
ignore the objects that are not FileInfos, and call base.Add(item) for
the ones that are.

Joshua Flanagan
http://flimflan.com/blog
John Spiegel wrote:
Hi all,

I'm trying to create a subclass of an ArrayList that is intended
specifically for holding FileInfo objects. My idea to restrict this is to
override the Add and AddRange methods to allow only FileInfo objects. In
AddRange, how can I verify that the collection passed is a collection of
FileInfo objects?

TIA,

John

Nov 22 '05 #2
Thanks, Joshua. Will look at that CollectionBase idea!

- John
"Joshua Flanagan" <jo**@msnews.com> wrote in message
news:u7**************@TK2MSFTNGP09.phx.gbl...
It is generally recommended to derive from CollectionBase instead of
ArrayList for your custom collections. Check out this article for some
hints:
http://www.c-sharpcorner.com/Code/20...ollections.asp

You can then create your own AddRange method that only accepts a FileInfo
array:

public void AddRange(FileInfo[] files)

If you want to stick with overriding the ArrayList, I don't think there is
anything you can do aside from looping over the collection and checking
the type of each object in the collection. It probably won't be the most
performant code, and you probalby lose the performance benefit of the
AddRange method.

public override void AddRange(System.Collections.ICollection c) {
foreach(object item in c){
if (!item is System.IO.FileInfo) {
throw new ArgumentException("");
}
base.AddRange(c);
}

Instead of throwing an exception if one item is bad, you could just ignore
the objects that are not FileInfos, and call base.Add(item) for the ones
that are.

Joshua Flanagan
http://flimflan.com/blog
John Spiegel wrote:
Hi all,

I'm trying to create a subclass of an ArrayList that is intended
specifically for holding FileInfo objects. My idea to restrict this is
to override the Add and AddRange methods to allow only FileInfo objects.
In AddRange, how can I verify that the collection passed is a collection
of FileInfo objects?

TIA,

John

Nov 22 '05 #3

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

Similar topics

2
by: John Spiegel | last post by:
Hi all, I'm trying to create a subclass of an ArrayList that is intended specifically for holding FileInfo objects. My idea to restrict this is to override the Add and AddRange methods to allow...
2
by: Steve Jorgensen | last post by:
I frequently find myself wanting to use class abstraction in VB/VBA code, and frankly, with the tacked-on class support in VB/VBA, there are some problems with trying to do that and have any...
1
by: emma middlebrook | last post by:
Hi I want to find out what objects are due to receive an event i.e. those that have added themselves as an event handler via +=. Yes, it's a little pointless perhaps (or can anyone give some...
2
by: MattC | last post by:
Hi, How can do runtime casting? MyCollection derives from ArrayList I will store lost of different objects that all derive from the same parent class. I then want to be able to pass in the...
7
by: Madhu Gopinathan | last post by:
Hi, I hope this is the right forum for this question. I am extending ICollection to create a Collection Type (say MyCollection) wherein I can control the types of objects being added to the...
3
by: Tor Inge Rislaa | last post by:
Finding name and type In the activate procedure of a form I want to write to the debug window, name and type of all controls at that actual form. Is there a smart way to do that? Allso for...
5
by: Simon | last post by:
Hi all, I am writing a windows application using vb.net on the 1.1 framework. We have in the application, some strongly typed collections that have been written as classes that do not inherit...
6
by: Hyun-jik Bae | last post by:
Is there any way how to get the item which has the most similar key in key-value collection object such as Dictionary<or SortedDictionary<> although there is no matching key? If there is none, is...
3
by: christopher | last post by:
Let me preface this with: I do have a good STL book on order from amazon as we speak. $70..ouch, but as much as I ask STL questions on here... In the meantime, I need a collection that can store...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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...

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.