PC Review


Reply
Thread Tools Rate Thread

Determine Number and Count of Unique Extensions

 
 
Edwin
Guest
Posts: n/a
 
      20th Oct 2008
What I am trying to do is come up with a way to determine how many unique
extensions there are within a directory and its sub directories. Within the
loop that recurses root folder, I am attempting to use a SortedList. Below
is the code:

private System.Collections.SortedList _ArrayListOfExtensions = new
SortedList(new CaseInsensitiveComparer());

System.Int32 myIndexOfValue = -1;
myIndexOfValue =
this._ArrayListOfExtensions.IndexOfKey(myCurrentFile.Extension.ToLower());

if (myIndexOfValue == -1)
{
this._ArrayListOfExtensions.Add(myCurrentFile.Extension.ToLower(), 1);
}

The Exception error that I am getting is:
"Unable to cast object of type 'System.Collections.DictionaryEntry' to type
'System.String'."

Any help would be appreciated as to how to correct this error. Also, if you
know of a better way (I have tried several) to accomplish the above, I am
open to your suggestions.

The results should be similar to:

15 .docx
17 .doc
14 .txt
7 .exe
etc...

Thanks!
Edwin
 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      20th Oct 2008
Edwin <(E-Mail Removed)> wrote:
> What I am trying to do is come up with a way to determine how many unique
> extensions there are within a directory and its sub directories. Within the
> loop that recurses root folder, I am attempting to use a SortedList. Below
> is the code:
>
> private System.Collections.SortedList _ArrayListOfExtensions = new
> SortedList(new CaseInsensitiveComparer());


SortedList isn't an ArrayList - it's a dictionary, basically.

Are you using .NET 3.5? LINQ would make this pretty much trivial...

--
Jon Skeet - <(E-Mail Removed)>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
 
Reply With Quote
 
Edwin
Guest
Posts: n/a
 
      20th Oct 2008
Thank you Jon for your reply,

I am using Visual Studio 2008 Professional with .Net 3.5

I appreciate your assistance.

Edwin

"Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Edwin <(E-Mail Removed)> wrote:
>> What I am trying to do is come up with a way to determine how many unique
>> extensions there are within a directory and its sub directories. Within
>> the
>> loop that recurses root folder, I am attempting to use a SortedList.
>> Below
>> is the code:
>>
>> private System.Collections.SortedList _ArrayListOfExtensions = new
>> SortedList(new CaseInsensitiveComparer());

>
> SortedList isn't an ArrayList - it's a dictionary, basically.
>
> Are you using .NET 3.5? LINQ would make this pretty much trivial...
>
> --
> Jon Skeet - <(E-Mail Removed)>
> Web site: http://www.pobox.com/~skeet
> Blog: http://www.msmvps.com/jon.skeet
> C# in Depth: http://csharpindepth.com


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      21st Oct 2008
Edwin <(E-Mail Removed)> wrote:
> Thank you Jon for your reply,
>
> I am using Visual Studio 2008 Professional with .Net 3.5
>
> I appreciate your assistance.


In which case I suggest you look at the overloads of Enumerable.GroupBy
which take an IEqualityComparer<TKey>.

--
Jon Skeet - <(E-Mail Removed)>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Count number of unique users =?Utf-8?B?VHJhY2V5?= Microsoft Access Queries 2 27th Jun 2006 01:40 PM
Count the number of unique records joshczinger@gmail.com Microsoft Excel Worksheet Functions 7 8th Mar 2006 07:33 AM
How do I count number of UNIQUE entries? andrew@obopay.com Microsoft Access 1 1st Dec 2005 12:07 AM
Count number of Unique values =?Utf-8?B?QWxhbg==?= Microsoft Excel Worksheet Functions 4 6th Jan 2005 08:05 PM
Conditional Unique Number Count Russel Microsoft Excel Worksheet Functions 2 17th Mar 2004 12:07 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:55 PM.