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

    Keys Again (VB.NET 2005)

    If you have a collection, for example,
    <pre> 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")
    </pre>

    Is there a way to retrieve the keys or convert c into a dictionary? The keys seem to be write-only! <img src=/S/drop.gif border=0 alt=drop width=23 height=23>
    <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: Keys Again (VB.NET 2005)

    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.

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

    Re: Keys Again (VB.NET 2005)

    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?
    <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: Keys Again (VB.NET 2005)

    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.

Posting Permissions

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