473,806 Members | 2,874 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1418
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(FileIn fo[] 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.IC ollection c) {
foreach(object item in c){
if (!item is System.IO.FileI nfo) {
throw new ArgumentExcepti on("");
}
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.co m> wrote in message
news:u7******** ******@TK2MSFTN GP09.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(FileIn fo[] 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.IC ollection c) {
foreach(object item in c){
if (!item is System.IO.FileI nfo) {
throw new ArgumentExcepti on("");
}
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
340
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 only FileInfo objects. In AddRange, how can I verify that the collection passed is a collection of FileInfo objects? TIA,
2
2281
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 type-safety as well. I thought I would share some of what I've come to think about this after dealing with it several times of late. First, an example. Let's say I have several classes, each with a string property called Name, and I have several...
1
2545
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 good uses for this?!!). How do I do this for an event on a class I implement? Also, how may I do this for an event in the .Net framework e.g. a control?
2
5481
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 object type and collection type I have a number of classes that derive from ArrayList and have if pick out on ly those i asked for.
7
2243
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 collection. Thus, my interface now looks like this public interface IMyCollection : ICollection { void Add (string toBeAdded);
3
1097
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 the entire application I want to print the name of all forms to the debug window. TIRislaa
5
4237
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 from collection base, but use an internal collection, to hold the objects and then implement IEnumerator, see example below,
6
1736
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 there any substitution class for enabling this? Please reply. Thanks in advance. Hyun-jik Bae
3
2535
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 an object that can be looked up by two differant key types. Is there an existing class or combination of classes that I can do this with? Requirements: I need a collection of elements with the following association: MyClass * - guarenteed to...
275
12440
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
9719
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10369
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10372
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10110
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7650
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5682
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3851
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.