Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - Converting String Pairs to IDictionary?

Reply
 
Old 04-29-2008   #1 (permalink)
coconet


 
 

Converting String Pairs to IDictionary?


I have a string like this

mystring = "color1:blue;color2:red";

I am tring to convert this into an IDictionary populated like this:
color1 blue
color2 red

My non-working syntax is

IDictionary<string,string> tempdict =
new Dictionary<string>(mystring.Split(';').ToDictionary()
).Split(':');

Help?



My System SpecsSystem Spec
Old 04-29-2008   #2 (permalink)
Anthony Jones


 
 

Re: Converting String Pairs to IDictionary?

"coconet" <coconet@xxxxxx> wrote in message
news:6vre14tild7q6q18tab1va3v5tm8c0cgsd@xxxxxx
Quote:

>
> I have a string like this
>
> mystring = "color1:blue;color2:red";
>
> I am tring to convert this into an IDictionary populated like this:
> color1 blue
> color2 red
>
> My non-working syntax is
>
> IDictionary<string,string> tempdict =
> new Dictionary<string>(mystring.Split(';').ToDictionary()
> ).Split(':');
>
Try this:- (untested)

IDictionary<string,string> tempdict = new Dictionary<string,string>()
foreach(string[] item in splitItems(mystring.Split(';')))
tempdict.Add(item[0], item[1]);

private static IEnumerable<string[]> splitItems(string input)
{
foreach (string item in input.Split(';'))
yield return item.Split(':');
}

--
Anthony Jones - MVP ASP/ASP.NET


My System SpecsSystem Spec
Old 04-30-2008   #3 (permalink)
coconet


 
 

Re: Converting String Pairs to IDictionary?




Thanks for the help. Unfortunately I am working with an instance class
so i can't do an extension method (I think that's what you're doing).



On Tue, 29 Apr 2008 22:10:00 +0100, "Anthony Jones"
<Ant@xxxxxx> wrote:
Quote:

>"coconet" <coconet@xxxxxx> wrote in message
>news:6vre14tild7q6q18tab1va3v5tm8c0cgsd@xxxxxx
Quote:

>>
>> I have a string like this
>>
>> mystring = "color1:blue;color2:red";
>>
>> I am tring to convert this into an IDictionary populated like this:
>> color1 blue
>> color2 red
>>
>> My non-working syntax is
>>
>> IDictionary<string,string> tempdict =
>> new Dictionary<string>(mystring.Split(';').ToDictionary()
>> ).Split(':');
>>
>
>Try this:- (untested)
>
>IDictionary<string,string> tempdict = new Dictionary<string,string>()
>foreach(string[] item in splitItems(mystring.Split(';')))
> tempdict.Add(item[0], item[1]);
>
>private static IEnumerable<string[]> splitItems(string input)
>{
> foreach (string item in input.Split(';'))
> yield return item.Split(':');
>}
My System SpecsSystem Spec
Old 04-30-2008   #4 (permalink)
Anthony Jones


 
 

Re: Converting String Pairs to IDictionary?

"coconet" <coconet@xxxxxx> wrote in message
news:lgvg14lsnaip1s5sq8pg2r7sirqpcfl8f1@xxxxxx
Quote:

>
>
>
> Thanks for the help. Unfortunately I am working with an instance class
> so i can't do an extension method (I think that's what you're doing).
>
>
>

Its not an extension method.

Just because you are working on code designed to run as an instance method
doesn't mean it can't call static methods. I tend to make any method that
doesn't need instance members static. As a private method it doesn't really
matter that much whether its marked as static or not. It just seems more
correct to me.


--
Anthony Jones - MVP ASP/ASP.NET
Quote:

> On Tue, 29 Apr 2008 22:10:00 +0100, "Anthony Jones"
> <Ant@xxxxxx> wrote:
>
Quote:

> >"coconet" <coconet@xxxxxx> wrote in message
> >news:6vre14tild7q6q18tab1va3v5tm8c0cgsd@xxxxxx
Quote:

> >>
> >> I have a string like this
> >>
> >> mystring = "color1:blue;color2:red";
> >>
> >> I am tring to convert this into an IDictionary populated like this:
> >> color1 blue
> >> color2 red
> >>
> >> My non-working syntax is
> >>
> >> IDictionary<string,string> tempdict =
> >> new Dictionary<string>(mystring.Split(';').ToDictionary()
> >> ).Split(':');
> >>
> >
> >Try this:- (untested)
> >
> >IDictionary<string,string> tempdict = new Dictionary<string,string>()
> >foreach(string[] item in splitItems(mystring.Split(';')))
> > tempdict.Add(item[0], item[1]);
> >
> >private static IEnumerable<string[]> splitItems(string input)
> >{
> > foreach (string item in input.Split(';'))
> > yield return item.Split(':');
> >}
>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Reading and assigning key/value pairs from XML PowerShell
Cast from one generic IDictionary to another .NET General
winrm nested key=value pairs PowerShell
Converting Get-ChildItems to string for processing. PowerShell
Converting date time format to string format PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46