Problem with SortedList

N

Nathan Sokalski

I am trying to make a SortedList using the following code:

Dim Poems As New SortedList(47)

Dim poemfiles As String() =
System.IO.Directory.GetFiles(Server.MapPath("poetry/poems/"))

For Each poemfile As String In poemfiles

Poems.Add(System.IO.File.OpenText(poemfile).ReadLine(), poemfile)

Next






My application makes it through all this code fine, and I have tested that
both parameters in the Poems.Add() method are Strings. However, when I try
to access the SortedList I recieve an error about the IComparer. I use the
following expression inside a loop to attempt to access it (j is the loop
control variable):

CStr(CType(Global.Poems(j), DictionaryEntry).Key)


Because I am somewhat unfamiliar with the IComparer class and the help files
didn't seem to be of much help, could someone tell me what I am doing wrong?
Thanks.
 
K

Karl Seguin

Try looping through it with:
For Each entry As DictionaryEntry In poems
Dim key As String = CStr(entry.Key)
Dim value As String = CStr(entry.Value)
Next

Also note that you aren't closing your openfile handlers...

For Each file As String In
IO.Directory.GetFiles("C:\karl\2005\HtmlClassGenerator\HtmlClassGenerator",
"*.cs")
Dim sr As StreamReader = IO.File.OpenText(file)
poems.Add(sr.ReadLine(), file)
sr.Close()
Next

AND that if two files start with the same line, you'll get an exception
because you can't add entries with the same key...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top