Results 1 to 5 of 5
  1. #1
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Get the Keys From a Generic Sorted List (C# 2005)

    If I have a SotedList
    SortedList<string,string> sList = ...

    and I want a string array of the Keys, then this code does that
    <pre>IList<string> ccList = sList.Keys;
    string[] cc = new string[cc.Count];
    ccList.CopyTo(cc,0);</pre>


    Is it possible to do this in a single line of code?
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  2. #2
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    1,862
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Get the Keys From a Generic Sorted List (C# 2005)

    Any parcicular reason you want to do this with only one line?

  3. #3
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Get the Keys From a Generic Sorted List (C# 2005)

    I'm in the process of converting a zillion lines of code from framework 1.1 to framework 2.0. As we convert to generics we are entering code like this a lot, so a one-liner would mean less time to convert. Plus, it's just a simple copy: should be one line.

    BTW, there was a typo in the code: here is a working example:
    <pre> private void button1_Click(object sender, EventArgs e)
    {
    SortedList<string,string> sList = new SortedList<string,string>();
    sList.Add("A", "1");
    sList.Add("B", "2");
    sList.Add("C", "3");
    //
    // Should be a one-liner for the next three lines
    IList<string> keyList = sList.Keys;
    string[] keys = new string[keyList.Count];
    keyList.CopyTo(keys, 0);
    }</pre>

    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  4. #4
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    1,862
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Get the Keys From a Generic Sorted List (C# 2005)

    I think the problem here is the CopyTo function. Since it is a void and expects a variable to be passed in, you're going to have to declare the array variable on its own line before calling the CopyTo method.

    Of course, you could always remove the line breaks and make it one long line (keeling the semi-colons).

    Also, have you looked into using a Code generator (such as My Generation or CodeSmith)? These are generally used in conjunction with a data tier, but can also be manipulated to generate based on any data source. It may take a bit of setup work, but you'll find the results to be staggering!

    BTW - I thought you have been a VB(.NET) guy in the past. Working with C# now? <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

  5. #5
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Get the Keys From a Generic Sorted List (C# 2005)

    >the problem here is the CopyTo function
    Right, it will take a different approach. The real problem is that IList does not have a ToArray method. I tried casting it into something else, but failed.

    > you could always remove the line breaks <img src=/S/disappointed.gif border=0 alt=disappointed width=15 height=15>

    > you have been a VB(.NET) guy
    At this point, I've forgotten more than I know about VB. For about a year, I've been creating tools for a Geographic Information System, ArcMap, using C#. Its object model is so horrific http://edndoc.esri.com/arcobjects/8.3/Diagrams/Map Layer Object Model.pdf, that I'll never complain about Office objects again.

    Eureka!
    <pre> string[] keys = new List<string>(sList.Keys).ToArray(); </pre>

    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •