2

I am trying to split my list of strings at each separate lines using the String.Split method but the both the method below and a regex method did not work. Instead, they returned the following result {0} 0. System.String[] instead of the actual array of strings. Please help to find the error(s) in the code below:

    string m_settings_temp;
    string[] m_settings;
    public void ShowSettingsGui() {
       var dialog = new OpenFileDialog { Filter = "Data Sources (*.ini)|*.ini*|All Files|*.*" };
       if (dialog.ShowDialog() != DialogResult.OK) return;
       m_settings_temp = File.ReadAllText(dialog.FileName);
       m_settings = m_settings_temp.Split(new [] { '\r', '\n' });
       //This regex method failed as well:  
       //m_settings = Regex.Split(m_settings_temp,"\r\n|\r|\n");
    }

    //The method below is to evaluate the output
    protected override void SolveInstance(IGH_DataAccess DA)
                    {
                        if (m_settings == null)
                        {
                            AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "You must declare some valid settings");
                            return;
                        }
                        DA.SetData(0, m_settings);
                    }

Thanks in advance!

3
  • 1
    The methods return an array of strings - not a single string. Commented Aug 13, 2012 at 10:28
  • how many files do you choose to read ? Commented Aug 13, 2012 at 10:32
  • @Oded this is why I am declaring string[] Commented Aug 13, 2012 at 11:19

3 Answers 3

7

just use ReadAllLines like this

m_settings = File.ReadAllLines(dialog.FileName);

this will give you a string[] with an element for each line in the selected file. If, after running this code, m_settings has no elements, the file you selected was empty.


If I wanted to interrgoate the contents of m_settings in a console app I might do somthing like.

for (var i = 0; i < m_settings.Length; i ++)
{
    Console.WriteLine(m_settings[i]);
}

This would output the content of array, one element at a time. If I used the implementation of ToString for am array, like this,

Console.WriterLine(m_settings);

I would get a string representation of the array's type and the number of elements it contains.


I suspect in your case you want to do somthing like

protected override void SolveInstance(IGH_DataAccess DA)
{
     if (m_settings == null || m_settings.Length == 0)
     {
         AddRuntimeMessage(
              GH_RuntimeMessageLevel.Warning,
              "You must declare some valid settings");

         return;
     }

     for (var i = 0; i < m_settings.Length; i ++)
     {
         DA.SetData(i, m_settings[i]);
     }
}
Sign up to request clarification or add additional context in comments.

9 Comments

+1 That is better than splitting it yourself. It keeps the code clean.
In some cases it's better to use File.ReadLines() (msdn.microsoft.com/en-us/library/dd383503) which will let you enumerate over all the lines in a file WITHOUT storing them all in memory. This can be important for very large files.
Thanks a lot but I am still getting the {0} 0. System.String[] as output instead of the actual string array.
@arthurmani, how are you evaluating the ouput? You don't show that in your sample.
Okay, I don't know enough about IGH_DataAccess to tell you exactly what is right. I suspect your orginal code is more correct. Possibly, when you access the settings you will need to do somthing like DA.GetData(0)[0] but I am just guessing. It all depends how array values are treated by your component framework.
|
1
m_settings = m_settings_temp
    .Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

Comments

1
string m_settings_temp;
string[] m_settings;
public void ShowSettingsGui()
{
    var dialog = new OpenFileDialog { Filter = "Data Sources (*.ini)|*.ini*|All Files|*.*" };
    if (dialog.ShowDialog() != DialogResult.OK) return;

    m_settings_temp = File.ReadAllText(dialog.FileName);
    m_settings = m_settings_temp.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.