• Keys Again (VB.NET 2005)

    Home » Forums » Developers, developers, developers » DevOps Lounge » Keys Again (VB.NET 2005)

    Author
    Topic
    #442791

    If you have a collection, for example,

            Dim c As New Collection
            c.Add("notepad.exe", "txt")
            c.Add("paint.exe", "bmp")
            c.Add("paint.exe", "dib")
            c.Add("wordpad.exe", "rtf")
    

    Is there a way to retrieve the keys or convert c into a dictionary? The keys seem to be write-only! drop

    Viewing 0 reply threads
    Author
    Replies
    • #1067063

      I think you are correct in that the key is write-only.

      The issue seems to be that the Collection class is part of the legacy Microsoft.VisualBasic library, rather than the .NET System.Collections namespace. If you were to use a native .NET based collection (that implemented the IDictionary interface or any other generic) this would be easy…

      Is the code sample part of the code you’re converting? If so, it may be as simple as changing Collection to something else (like Hashtable). Of course, the problem then would be the order of the parameters. Hashtable expects key as the first parameter, not second, and only allows unique keys…

      Hope this helps.

      • #1067101

        Thanks, Mark for the confirmation! I was just trying to help a friend. It turned out even worse: he had developed a class using that keyed collection as a base class and then wanted to sort it by keys. You wonder why they didn’t seal all of the VB library. Like you, I told him to rewrite it using generics. Thanks again.

        But, I’m still intrigued by how Quick Watch can see the keys and that each entry in the collection is a key value pair. Have you ever used System.Reflection? Could this be of use here?

        • #1067115

          That is interesting – When stepping through the code in the de###### (VB 2005) it does indeed allow you to drill down into the collection to see the keys as well as the values. The de###### must be casting the Collection as a different type because it exposes an Items property and each item as a KeyValuePair – neither of which is normally a part of the Collection class. Interesting!

          I’m not sure what value Reflection will be in this situation. And unfortunately, it doesn’t appear that you can easily convert the Collection into a more friendly .NET type (although I hope I’m wrong on that one).

          As a side note (and I don’t mean to offend anyone), this is one of those situations in which using legacy VB techniques in the .NET environment are actually counter-productive. As a now-hardcore C# developer (formerly VB and VB.NET), I hear a lot of VB(.NET) bashing from other developers. I must often clarify that VB.NET is essentially identical to C# in its capabilities as a language. The problem is actually caused when VB.NET developers employ legacy techniques and architectures that do not leverage the newest (and best) .NET components, practices, and such. Perhaps this is perpetuated by Microsoft’s catering to that market by continuing to support the older libraries in even the newest .NET builds. Bottom line – hopefully all developers will do their homework and move forard with the language-appropriate development techniques even if the older techniques are still supported. Just my 2 cents.

    Viewing 0 reply threads
    Reply To: Keys Again (VB.NET 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: