Question

I have a .ini file the .ini:

;app ...

    [MSGS]

    title#0 = first title.. 
    message#0 = first message 

    title#1 = second title
    message#1 = second message 

    title#2 = third title
    message#2 = third message  

I'm using the Nini lib for parsing. I need read it to a dictionary.

I tried this:

 public Dictionary<string, string> Read()
        {
            try
            {
                Dictionary<string, string> result = new Dictionary<string, string>();
                IConfigSource src = config;
                IConfig values = src.Configs["MSGS"];
                string[] keys = values.GetKeys();

                for (int count = keys.Length / 2,i = 0, j = 1; 
                        i < count; i++, 
                        j = i + 1)
                {
                    string titleKey = keys[i];
                    string messageKey = keys[j];
                    string titleVal = values.Get(titleKey);
                    string messageVal = values.Get(messageKey);
                    result.Add(titleVal, messageVal);

                }
            }
            catch (Exception)
            {

            }
            return null;
        }

The output is:

first title.. : first message
first message : second title
second title : second message

I want:

first title.. : first message
second title : second message
third title : third message

How I do this? Thanks in advance. :)

Was it helpful?

Solution

            for (int i = 0; i < keys.Length; i += 2)
            {
                string titleKey = keys[i];
                string messageKey = keys[i+1];
                string titleVal = values.Get(titleKey);
                string messageVal = values.Get(messageKey);
                result.Add(titleVal, messageVal);
            }

OTHER TIPS

There are too many variables in your loop - the j and count are not needed and only serve to confuse matters:

for (i = 0; i < keys.Length; i += 2)
{
  string titleKey = keys[i];
  string messageKey = keys[i + 1];
  string titleVal = values.Get(titleKey);
  string messageVal = values.Get(messageKey);
  result.Add(titleVal, messageVal);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top