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

    Home » Forums » Developers, developers, developers » DevOps Lounge » Get the Keys From a Generic Sorted List (C# 2005)

    • This topic has 4 replies, 2 voices, and was last updated 18 years ago.
    Author
    Topic
    #442315

    If I have a SotedList
    SortedList sList = …

    and I want a string array of the Keys, then this code does that

    IList ccList = sList.Keys;
    string[] cc = new string[cc.Count];
    ccList.CopyTo(cc,0);

    Is it possible to do this in a single line of code?

    Viewing 0 reply threads
    Author
    Replies
    • #1064370

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

      • #1064387

        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:

                private void button1_Click(object sender, EventArgs e)
                {
                    SortedList sList = new SortedList();
                    sList.Add("A", "1");
                    sList.Add("B", "2");
                    sList.Add("C", "3");
                    // 
                    // Should be a one-liner for the next three lines
                    IList keyList = sList.Keys;
                    string[] keys = new string[keyList.Count];
                    keyList.CopyTo(keys, 0);
                }
        • #1064389

          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? smile

          • #1064417

            >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 disappointed

            > 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[/url], that I’ll never complain about Office objects again.

            Eureka!

                        string[] keys = new List(sList.Keys).ToArray(); 
    Viewing 0 reply threads
    Reply To: Get the Keys From a Generic Sorted List (C# 2005)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: