简体   繁体   中英

Creating a dictionary of Func

I have a method which writes a DataGridView into a text file:

private void textToolStripMenuItem_Click(object sender, EventArgs e)
{
  SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
  saveFileDialog1.FilterIndex = 2;
  saveFileDialog1.RestoreDirectory = true;
  saveFileDialog1.FileName = DateTime.Now.ToString("yyyyMMddhhmm") + "_icmquery_" + GetTabName() + ".txt";
  if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  { 
    using (FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.OpenOrCreate))
    {
      switch (mainTabGroup.SelectedTab.Name.ToString())
      {
        case "findScriptsTab":
          WriteTextFile(fs, findScriptsDataGrid);
          break;
        case "dialNumberAuditTab":
          WriteTextFile(fs, findScriptsDataGrid);
          break;
        case "calltypeRequalificationTab":
          WriteTextFile(fs, ctrDataGrid);
          break;
        case "targetAuditTab":
          WriteTextFile(fs, targetAuditDataGrid);
          break;
      }
    }
  }
}
public void WriteTextFile(FileStream fs, DataGridView dataGrid)
{
  using (TextWriter tw = new StreamWriter(fs))
  {
    foreach (DataGridViewRow row in dataGrid.Rows)
    {
      string line = string.Empty;
      foreach (DataGridViewCell cell in row.Cells)
      {
        line = line + cell.Value + ",";
      }
      line = line.TrimEnd(',');
      tw.WriteLine(line);
    }
  }
}

But instead of using the large switch statement in textToolStripMenuItem_Click() I would like to define a dictionary in my form. I have tried:

private Dictionary<TabPage, Func<FileStream, DataGridView>> WriteTextFileByTab(FileStream fs) = new Dictionary<TabPage, Func<FileStream, DataGridView>>()
{
  {findScriptsTab, WriteTextFile(fs, findScriptsDataGrid)}
};

But visual studios won't even register this as a valid thing. I typed this in manually but none of the intense would fill anything in. When I hover over "findScriptsTab" it says its a field by used like a type, and WriteTextFile says the same thing. Any idea on how I could properly convert the switch statement to a dictionary so I can just say: WriteTextFileByTab[mainTabGroup.SelectedTab]; ?

You cannot define the parameter to the method for each item in the dictionary before you define the dictionary itself. You need to declare the parameter's identifier when constructing each value of the dictionary:

private Dictionary<TabPage, Func<FileStream, DataGridView>> WriteTextFileByTab 
    = new Dictionary<TabPage, Func<FileStream, DataGridView>>()
{
    {findScriptsTab, fs =>  WriteTextFile(fs, findScriptsDataGrid)}
};

Of course if findScriptsTab and findScriptsDataGrid are instance fields, as I suspect that they are, you won't be able to use them when initializing another instance field. As such, you'll need to initialize this dictionary in the constructor, not when declaring it.

It also looks like the delegates in your dictionary (based on your implementation of WriteTextFile ) don't actually need to return a DataGridView so the appropriate delegate to use is actually an Action<FileStream> , not a Func<FileStream, DataGridView> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM