CAST NAMEVALUECOLLECTION TO DICTIONARY

The Dictionary is not always going to contain strings. I need a generic dictionary to work with. Stack Overflow works best with JavaScript enabled. By using our site, you acknowledge that you have read and understand our Cookie Policy , Privacy Policy , and our Terms of Service. Dmitriy Dokshin Dmitriy Dokshin 4 That is what I am seeking help with here.

Lee Lee k 14 Post as a guest Name. Kobojunkie – You can’t copy it to a generic dictionary, since the key and value types can be anything. Post Your Answer Discard By clicking “Post Your Answer”, you acknowledge that you have read our updated terms of service , privacy policy and cookie policy , and that your continued use of the website is subject to these policies. If you know that your dictionary is always going to contain strings, specify it to contain strings instead of making your class generic:. But it is not a public property. You could provide delegates to your copy method to do this: The Dictionary is not always going to contain strings.

C# (CSharp) System.Collections.Specialized NameValueCollection.ToDictionary Examples

It’s very confusing, because presumably this is in a generic type declaring TKeyTValue type parameters – and your method declares those type parameters too. Dmitriy Dokshin Dmitriy Dokshin 4 It doesn’t make sense to use generics here since you can’t assign string s to some arbitrary generic type: You could provide delegates to your copy method to do this: Kobojunkie Kobojunkie 2, 22 86 It will only compile if T is string or a less-derived type. If you know that your dictionary is always going to contain strings, specify it to contain strings instead of making your class generic: Add to that the fact that the keys will always be strings Chobits Chobits 2 The Dictionary is not always going to contain strings.

  LG M227WDP USB MOVIE

You should not forget about EqualityComparer. Reed Copsey Reed Copsey k 58 But it is not a public property. Post as a guest Name.

DotNet Programming World: Convert NameValueCollection to Dictionary

I need a generic dictionary to work with. Kobojunkie There is no “general purpose” way to do this – you need some type of method to do the conversion. Jonathan Freeland 3, 3 21 You could convert your strings to the target type, but that isn’t clear dichionary your question.

By clicking “Post Your Answer”, you acknowledge that you have read namevaluecollection updated terms of serviceprivacy policy and cookie policyand that your continued use of the website is subject to these policies.

NameValueCollection contains String keys and values and so I simply want to provide here a method to allow copying of those to a generic dictionary. That is what I am seeking help with here.

I want to be able to copy those over to a generic dictionary. You would then need to pass a delegate in that would perform the conversion from string to TValue and string to TKey.

So, you should use reflection to get it.

By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. Trying to copy values from an existing NameValueCollection object to a Dictionary.

Email Required, but never shown. Lee Lee k 14 Sign up using Facebook.

My second option shows how to write the method so this can be passed in as a delegate. Kobojunkie – You can’t copy it to a generic dictionary, since the key and value types can be anything. It doesn’t make namevaluecpllection to use generics here since you can’t assign string s to some arbitrary generic type:. Sign up or log in Sign up using Google.

  PAGANINI IL VIOLINISTA DEL DIAVOLO FILM STREAMING

Stack Overflow works best with JavaScript enabled. Post Your Answer Discard By clicking “Post Your Answer”, you acknowledge that you have read our updated terms of serviceprivacy policy and cookie policyand that your continued use of the website is subject to these policies. If you know that your dictionary is always going to contain strings, specify it to contain strings instead of making your class generic:. Again, what would this do if TKey isn’t namevakuecollection I simply want it to also be able to copy values from NameValueCollection object in this method.

I have the following code below to do that but seems the Add does not accept that my keys and values are as Strings. NameValueCollection has String keys and values.

Sign up using Casg and Password. ConvertFromString col[name] ; dict. I don’t believe that to be correct. You could provide delegates to your copy method to do this:. Extension method plus linq: Namevaluecollecrion you need this to be a generic class, and hold generic data, you need some way to convert from string to TKey and dictionarg to TValue.