0

I have a class Helper with 2 methods

public static List<string> GetCountryName()
{
    List<string> CountryName = new List<string>();
    using (var sr = new StreamReader(@"Country.txt"))
    {
        string line;
        while ((line = sr.ReadLine()) != null)
        {
            int index = line.LastIndexOf(" ");
            CountryName.Add(line.Substring(0, index));
        }
    }
    return CountryName;
}

public static List<string> GetCountryCode()
{
    List<string> CountryCode = new List<string>();
    using (var sr = new StreamReader(@"Country.txt"))
    {
        string line;
        while ((line = sr.ReadLine()) != null)
        {
            int index = line.LastIndexOf(" ");
            CountryCode.Add(line.Substring(index + 1));
        }
    }
    return CountryCode;
}

I bind these return values with my WPF ComboBox as follows

ddlCountryName.ItemsSource = Helper.GetCountryName();
ddlCountryCode.ItemsSource = Helper.GetCountryCode();

I wanted to return these List in a single method and went through these links

Tuple Class

https://stackoverflow.com/a/10278769

After going through it I tries like this but could not able to make it properly from line no-3 Tuple<List<string>> CountryName = new Tuple<List<string>>

public static Tuple<List<string>,List<string>> GetAllData()
{
    Tuple<List<string>> CountryName = new Tuple<List<string>>
    List<string> CountryCode = new List<string>();
    using (var sr = new StreamReader(@"Country.txt"))
    {
        string line;
        while ((line = sr.ReadLine()) != null)
        {
            int index = line.LastIndexOf(" ");
            CountryName.Add(line.Substring(0, index));
            CountryCode.Add(line.Substring(index + 1));
        }
    }
    return CountryName;
    return CountryCode;
}

Please help me to return List item wise and bind in the ItemsSource as per the below code

ddlCountryName.ItemsSource = Helper.GetCountryName();
Community
  • 1
  • 1
Sabyasachi Mishra
  • 1,677
  • 2
  • 31
  • 49
  • It would make a whole lot more sense to combine these into a single class containing the name and the code together and then split them apart using a LINQ `Select` statement just for the display. – Ian Mercer May 11 '16 at 05:41

2 Answers2

1

A Tuple is a pack of two or more values and the types of these values are specified through generic parameters when you create a Tuple object. You are creating a Tuple of a single value, which has no meanings. Try creating it like this:

Tuple<List<string>, List<string>> CountryName = new Tuple<List<string>, List<string>>();

Also note that a function cannot have more than one return statements. You should add both your lists to the Tuple object and then return it in one go.

Your final function will be something like (keep your two existing functions and create a new function that calls them):

public static Tuple<List<string>,List<string>> GetAllData()
{
  return new Tuple<List<string>, List<string>>(GetCountryName(), GetCountryCode());
}

An alternate approach:

public static List<Tuple<string, string>> GetAllData()
{
  List<Tuple<string, string>> Result = new List<Tuple<string, string>>();

  using (var sr = new StreamReader(@"Country.txt"))
  {
    string line;
    while ((line = sr.ReadLine()) != null)
    {
        int index = line.LastIndexOf(" ");
        var Name = line.Substring(0, index);
        var Code = line.Substring(index + 1);
        Result.Add(new Tuple<string, string>(Name, Code));
    }
  }

  return Result;
}

In the first case, you do your binding directly to the Tuple members:

ddlCountryName.ItemsSource = Result.Item1;
ddlCountryCode.ItemsSource = Result.Item2;

In the second case, you can do some further linq to get your individual lists from the returned object:

ddlCountryName.ItemsSource = Result.Select(x => x.Item1).ToArray();
ddlCountryCode.ItemsSource = Result.Select(x => x.Item2).ToArray();
dotNET
  • 33,414
  • 24
  • 162
  • 251
  • This gave error as There is no argument given that corresponds to required formal pareameter 'item1' of `Tuple, List>()` – Sabyasachi Mishra May 11 '16 at 05:36
  • The last method is perfect and Result returns Data in Items1 and Items2. But how would I bind this in my list as per this code `ddlCountryName.ItemsSource = Helper.GetAllData();` – Sabyasachi Mishra May 11 '16 at 05:57
1

try this (not tested)

public static Tuple<List<string>, List<string>> GetAllData()
{
    List<string> CountryName = new List<string>();
    List<string> CountryCode = new List<string>();
    using (var sr = new StreamReader(@"Country.txt"))
    {
        string line;
        while ((line = sr.ReadLine()) != null)
        {
            int index = line.LastIndexOf(" ");
            CountryName.Add(line.Substring(0, index));
            CountryCode.Add(line.Substring(index + 1));
        }
    }
    return new Tuple<List<string>, List<string>>(CountryName,CountryCode);
}
user3598756
  • 28,893
  • 4
  • 18
  • 28