Problem with SortedList

Discussion in 'ASP .Net' started by Nathan Sokalski, May 30, 2005.

  1. 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.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/
    Nathan Sokalski, May 30, 2005
    #1
    1. Advertising

  2. Nathan Sokalski

    Karl Seguin Guest

    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!)
    "Nathan Sokalski" <> wrote in message
    news:u$...
    >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.
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
    >
    Karl Seguin, May 30, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Weidenfeller

    Re: SortedList ?

    Thomas Weidenfeller, Jul 1, 2003, in forum: Java
    Replies:
    3
    Views:
    2,861
    Tim Tyler
    Jul 10, 2003
  2. Phil Hanna

    Re: SortedList ?

    Phil Hanna, Jul 2, 2003, in forum: Java
    Replies:
    0
    Views:
    819
    Phil Hanna
    Jul 2, 2003
  3. B-rad the Beat Nick

    SortedList: best practices with java 1.5

    B-rad the Beat Nick, Nov 4, 2005, in forum: Java
    Replies:
    5
    Views:
    113,027
    Roedy Green
    Nov 5, 2005
  4. sck10
    Replies:
    4
    Views:
    420
    sck10
    Sep 26, 2006
  5. jc
    Replies:
    1
    Views:
    704
    sloan
    Jul 15, 2008
Loading...

Share This Page