|
-
Sep 6th, 2011, 12:37 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] Quick dictionary question
When I initialize a new dictionary can I specify the key/value pairs on the same line? Thanks...
-
Sep 6th, 2011, 12:47 PM
#2
Re: Quick dictionary question
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 6th, 2011, 12:48 PM
#3
Thread Starter
Frenzied Member
Re: Quick dictionary question
-
Sep 6th, 2011, 12:52 PM
#4
Re: Quick dictionary question
ok. but where are you getting the data to put in the dictionary from?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 6th, 2011, 12:57 PM
#5
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.
-
Sep 6th, 2011, 12:58 PM
#6
Thread Starter
Frenzied Member
Re: Quick dictionary question
Yes, that's what I was looking for. Thanks!
-
Sep 6th, 2011, 01:24 PM
#7
Re: Quick dictionary question
that works great in .net4, but in .net3.5:
vb Code:
Dim items(,) As Object = {{1, "Home"}, _
{2, "Products"}, _
{3, "News"}, _
{4, "Contact Us"}}
Dim d As Dictionary(Of Integer, String) = Enumerable.Range(0, items.GetLength(0)).Select(Function(i) _
New With { _
.int = CInt(items(i, 0)), _
.str = items(i, 1).ToString}).ToDictionary(Function(kvp) kvp.int, Function(kvp) kvp.str)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 6th, 2011, 08:35 PM
#8
Re: Quick dictionary question
I think .paul. just proved that in 3.5 the answer is "No, just call Add afterwards".
-
Sep 7th, 2011, 06:41 AM
#9
Thread Starter
Frenzied Member
Re: Quick dictionary question
 Originally Posted by Sitten Spynne
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"}}
-
Sep 7th, 2011, 08:21 AM
#10
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
-
Sep 10th, 2011, 09:57 AM
#11
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.
-
Sep 11th, 2011, 06:28 PM
#12
Re: [RESOLVED] Quick dictionary question
 Originally Posted by Sitten Spynne
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>
-
Sep 11th, 2011, 09:41 PM
#13
Re: [RESOLVED] Quick dictionary question
Shouldn't it have been
Code:
Dictionary<Foo<T>, List<Foo<T>>>
-tg
-
Sep 11th, 2011, 10:09 PM
#14
Re: [RESOLVED] Quick dictionary question
Err... yes, yes it should.
 Originally Posted by Sitten Spynne
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|