Question

Can a SortedList take two keys and return one value?

I have standard of type SortedList<string, double>

But now I need pass two keys to find the value SortedList<string, string, double>.

Would this be possible? If not, please tell me some other solutions, I built a SortedList using string and double, but now I found out that I need to "call" that double based on two strings.

List<string> StatsCheckBoxList = new List<string>();
List<string> PeriodCheckBoxList = new List<string>();


 if (checkBox7.Checked)
    PeriodCheckBoxList.Add(checkBox7.Text);
 if (checkBox8.Checked)
    PeriodCheckBoxList.Add(checkBox8.Text);
 if (checkBox9.Checked)
    PeriodCheckBoxList.Add(checkBox9.Text);
 if (checkBox10.Checked)
    PeriodCheckBoxList.Add(checkBox10.Text);

 if (checkBox19.Checked)
    StatsCheckBoxList.Add(checkBox19.Text);
 if (checkBox35.Checked)
    StatsCheckBoxList.Add(checkBox35.Text);
 if (checkBox34.Checked)
    StatsCheckBoxList.Add(checkBox34.Text);


// print the name of stats onto the first column:
        int l = 0;
        foreach (string Stats in StatsCheckBoxList)
                {

                    NewExcelWorkSheet.Cells[ProductReturnRawData.Count + PeriodCheckBoxList.Count + 20 + l, 1] = Stats;

                                l++;
                }

  // print the time period of each stats onto the row above:
  int h = 0;
  foreach (string period in PeriodCheckBoxList)
             {

               NewExcelWorkSheet.Cells[ProductReturnRawData.Count + PeriodCheckBoxList.Count + 19, 2 + h] = period;

              h++;
              }

// this is a data table, now i have printed the statistic names to first column based on user selection, i also printed period based on user selection to the top row. Now i need to call the value of the stats based on which stats and period have been selected. So i need to pass two keys to my SortedList. Something like:

NewExcelWorkSheet.Cells[ProductReturnRawData.Count + 27, 2] = Convert.ToString(productReturnValue["3 Months"]); 

Here productReturnValue is a SortedList which take a string key and return a double value.

Was it helpful?

Solution

You need a Tuple of arity 2:

public SortedList<Tuple<string,string>,double> mySortedList ;

Though you'll probably have to provide it with a custom comparer, something like:

class My2TupleComparer : IComparer<Tuple<string,string>
{
  public int Compare(Tuple<string,string> x, Tuple<string,string> y )
  {
    int cc ;
    if      ( x == null && y == null ) cc =  0 ;
    else if ( x == null && y != null ) cc = -1 ;
    else if ( x != null && y == null ) cc = +1 ;
    else /* ( x != null && y != null ) */
    {
      cc = string.Compare(x.Item1 , y.Item1 , StringComparison.OrdinalIgnoreCase ) ;
      if ( cc == 0 )
      {
        cc = String.Compare( x.Item2 , y.Item2 , StringComparison.OrdinalIgnoreCase ) ;
      }
    }
    return cc ;
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top