0

In the code below, I am trying to assign the array Filepaths to the variable m_settings but Filepaths is not recognized outside the LINQ method. How can I store the content of FilePaths in a variable that i can use in the SolveInstance method?

public void ShowSettingsGui()
{
    var dialog = new OpenFileDialog()
    {
        Multiselect = true,
        Filter = "Data Sources (*.ini)|*.ini*|All Files|*.*"
    };
    if (dialog.ShowDialog() != DialogResult.OK) return;
    var paths = dialog.FileNames;
    var FilePaths = paths.ToDictionary(filePath => filePath, File.ReadAllText);
}

private string[] m_settings = Filepaths;  

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);
}
1
  • 1
    Can you not just assign m_settings in the ShowSettingsGui method and use m_settings in SolveInstance? Commented Aug 13, 2012 at 1:17

2 Answers 2

3

I could be reading into it too much but I don't think you need FilePaths, you can just set m_settings directly...

private Dictionary<string, string> m_settings;  

public void ShowSettingsGui()
{
    var dialog = new OpenFileDialog()
    {
        Multiselect = true,
        Filter = "Data Sources (*.ini)|*.ini*|All Files|*.*"
    };
    if (dialog.ShowDialog() != DialogResult.OK) return;
    var paths = dialog.FileNames;
    m_settings = paths.ToDictionary(filePath => filePath, File.ReadAllText);
}

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);
}

You also need to ensure SolveInstance is called after ShowSettingsGui, otherwise m_settings will always be null

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot, do you know why I get the following output and not the dictionnary in itself? '{0} 0. System.Collections.Generic.Dictionary`2[System.String,System.String]'
@arthurmani, you are only after a single file. Don't use a dictionary. just do string m_settings; and m_settings = File.ReadAllText(dialog.FileName);. The output is like that because that's what Dictionary.ToString returns.
@arthurmani I've updated my previous answer to show you how.
1
public void ShowSettingsGui()
{
    var dialog = new OpenFileDialog()
    {
        Multiselect = true,
        Filter = "Data Sources (*.ini)|*.ini*|All Files|*.*"
    };
    if (dialog.ShowDialog() != DialogResult.OK) return;
    var paths = dialog.FileNames;
    var FilePaths = paths.ToDictionary(filePath => filePath, File.ReadAllText);

    // You need to add this
    this.m_settings  = FilePaths;
}

// You also need to change the type of m_settings from string[] to Dictionary<string, string>
private Dictionary<string, string> m_settings = Filepaths;  

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);
}

2 Comments

Thanks a lot but I get the following error:Cannot implicitly convert type 'System.Collections.Generic.Dictionary<string,string>' to 'string[]' and The name 'Filepaths' does not exist in the current context
You've got a floating member declared in between the methods. Firstly, move that to the top. Once you do that, you'll realise that Filepaths doesn't exist at that level and so assigning m_settings to Filepaths is invalid.

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.