Results 1 to 14 of 14

Thread: [RESOLVED] Quick dictionary question

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Resolved [RESOLVED] Quick dictionary question

    When I initialize a new dictionary can I specify the key/value pairs on the same line? Thanks...

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Quick dictionary question

    you can with LINQ

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: Quick dictionary question

    can you post an example?

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Quick dictionary question

    ok. but where are you getting the data to put in the dictionary from?

  5. #5
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Quick dictionary question

    Collection Initializers Overview covers all of the cases.

    From the "VB can't do anything without introducing extra keywords" department:
    Code:
    Dim lookup = New Dictionary(Of Integer, String) From {{1, "One"}, {2, "Two"}}
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: Quick dictionary question

    Yes, that's what I was looking for. Thanks!

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Quick dictionary question

    that works great in .net4, but in .net3.5:

    vb Code:
    1. Dim items(,) As Object = {{1, "Home"}, _
    2.                           {2, "Products"}, _
    3.                           {3, "News"}, _
    4.                           {4, "Contact Us"}}
    5.  
    6. Dim d As Dictionary(Of Integer, String) = Enumerable.Range(0, items.GetLength(0)).Select(Function(i) _
    7.                                         New With { _
    8.                                         .int = CInt(items(i, 0)), _
    9.                                         .str = items(i, 1).ToString}).ToDictionary(Function(kvp) kvp.int, Function(kvp) kvp.str)

  8. #8
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Quick dictionary question

    I think .paul. just proved that in 3.5 the answer is "No, just call Add afterwards".

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: Quick dictionary question

    Quote Originally Posted by Sitten Spynne View Post
    Code:
    Dim lookup = New Dictionary(Of Integer, String) From {{1, "One"}, {2, "Two"}}

    Just a small correction to SS's code, it should be:

    Code:
    Dim lookup As New Dictionary(Of Integer, String) From {{1, "One"}, {2, "Two"}}

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Quick dictionary question

    With Infer On, it's the same thing. This is one of those cases where I have no objection to Infer being set to on, since Lookup can only ever be a dictionary.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: [RESOLVED] Quick dictionary question

    Yeah, I used to hate type inference and avoid it. Then I started having to work with generic collections that contain collections of generic types. Here's something I had to play with:
    Code:
    Dictionary(Of Foo(Of T), List(Of Foo(Of T)))
    One can only type that so many times before wishing for simple preprocessor macros .
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  12. #12
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: [RESOLVED] Quick dictionary question

    Quote Originally Posted by Sitten Spynne View Post
    Code:
    Dictionary(Of Foo(Of T), List(Of Foo(Of T)))
    One can only type that so many times before wishing for simple preprocessor macros .
    Or C#'s syntax:

    Code:
    Dictionary<Foo<T>, List<T>>
    How do you stand all those 'Of's floating around? Yuck.</flamebait>

  13. #13
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [RESOLVED] Quick dictionary question

    Shouldn't it have been
    Code:
    Dictionary<Foo<T>, List<Foo<T>>>
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  14. #14
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: [RESOLVED] Quick dictionary question

    Err... yes, yes it should.

    Quote Originally Posted by Sitten Spynne View Post
    One can only type that so many times before wishing for simple preprocessor macros .
    Apparently once, in my case?


Posting Permissions

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



Click Here to Expand Forum to Full Width