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?

Re: Converting String Pairs to IDictionary? by Anthony

Anthony
Tue Apr 29 16:10:00 CDT 2008

"coconet" <coconet@community.nospam> wrote in message
news:6vre14tild7q6q18tab1va3v5tm8c0cgsd@4ax.com...
>
> 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



Re: Converting String Pairs to IDictionary? by coconet

coconet
Wed Apr 30 09:12:38 CDT 2008




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@yadayadayada.com> wrote:

>"coconet" <coconet@community.nospam> wrote in message
>news:6vre14tild7q6q18tab1va3v5tm8c0cgsd@4ax.com...
>>
>> 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(':');
>}


Re: Converting String Pairs to IDictionary? by Anthony

Anthony
Wed Apr 30 16:58:58 CDT 2008

"coconet" <coconet@community.nospam> wrote in message
news:lgvg14lsnaip1s5sq8pg2r7sirqpcfl8f1@4ax.com...
>
>
>
> 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

> On Tue, 29 Apr 2008 22:10:00 +0100, "Anthony Jones"
> <Ant@yadayadayada.com> wrote:
>
> >"coconet" <coconet@community.nospam> wrote in message
> >news:6vre14tild7q6q18tab1va3v5tm8c0cgsd@4ax.com...
> >>
> >> 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(':');
> >}
>