N K
N K

Reputation: 307

Return multiple list

The following lists are created in class named "Entities" and they are private. I would like to return them (and use in another class)what I can do by :

    public List<string> getCMList()
    {
        return exceptionsCM;
    }

But if there is 10 list, I have to write a public "get" method for all of them. Is it possible to write a method, that takes a string as an input, and returns that list ? Should be something similar to ..

public List<string> getList(.....)
{
     return exceptionCM;
} //if we call getList(exceptionCM).


        exceptionsCM = new List<string>();
        exceptionsCM.Add("CM12");
        exceptionsCM.Add("CM701");
        exceptionsCM.Add("CM901/CM30");
        exceptionsCM.Add("CM901K");
        exceptionsCM.Add("CM1101");
        exceptionsCM.Add("CM1101K");

        //List with Multi Mill exceptions, they are included in the new MultiB
        exceptionsMultiB = new List<string>();
        exceptionsMultiB.Add("Multi650/450");
        exceptionsMultiB.Add("Multi660/630");
        exceptionsMultiB.Add("Multi650/800");
        exceptionsMultiB.Add("Multi650/1000");

        //List with Multi Mill exceptions, they are included in the new Multi01
        exceptionsMulti01 = new List<string>();
        exceptionsMulti01.Add("Multi301");
        exceptionsMulti01.Add("Multi601");
        exceptionsMulti01.Add("Multi801");
        exceptionsMulti01.Add("Multi1001");

Upvotes: 0

Views: 6239

Answers (4)

thersch
thersch

Reputation: 1396

Use dictionary and enum.

Using enum for list keys instead of a string makes your code more robust for key renamings. I.e. when you rename MultiB to MultiB1 in Entities class then compiler would not show any errors and warnings about calls to GetList("MultiB"). In case of using enum compiler would show you these errors.

internal class Entities
{
    private Dictionary<ListKey, List<string>> _liststDict =
      new Dictionary<ListKey, List<string>>
        {
          {ListKey.Cm, new List<string> {"CM12", "CM701", "CM901/CM30", "CM901K", "CM1101", "CM1101K"}},
          {ListKey.MultiB, new List<string> {"Multi650/450", "Multi660/630", "Multi650/800", "Multi650/1000"}},
          {ListKey.Multi01, new List<string> {"Multi301", "Multi601", "Multi801", "Multi1001"}}
        };

    public List<string> GetList(ListKey key)
    {
      return _liststDict[key];
    }

    internal enum ListKey
    {
      Cm,
      MultiB,
      Multi01
    }
}


internal class EntitiesTester
{
    public static void Do()
    {
      Entities entities = new Entities();

      Console.Out.WriteLine("CM count = {0}", entities.GetList(Entities.ListKey.Cm).Count);
      Console.Out.WriteLine("MultiB count = {0}", entities.GetList(Entities.ListKey.MultiB).Count);
      Console.Out.WriteLine("Multi01 count = {0}", entities.GetList(Entities.ListKey.Multi01).Count);
    }
}

Upvotes: 0

slawekwin
slawekwin

Reputation: 6310

You could put them all in a static Dictionary> like that

Dictionary<string, List<string>> ListstDict = new Dictionary<string, List<string>>();
ListsDict.Add("exceptionsCM", exceptionsCM);
...

Then you could write a method like so:

public List<string> GetList(string name)
{
    return ListsDict.ContainsKey(name) ? ListsDict[name] : null;
}

But then again, what's the point of this lists being private if you can get them that simply? It would be easier to just declare them as public properties with private setter

public List<string> exceptionsCM { get; private set; }

Upvotes: 2

DanielR
DanielR

Reputation: 724

You can put all your lists in a dictionary:

// your code here
dictionary = new Dictionary<string, List<string>>();
dictionary.Add("exceptionsCM", exceptionsCM);

Then the getList method is as simple as:

public List<string> getList(string name)
{
    return dictionary[name];
}

Upvotes: 2

oleksii
oleksii

Reputation: 35925

You can use a Dictionary<K,V> class for this

private Dictionary<string, List<<string>> dictionary = 
    new Dictionary<string, List<<string>>();

//fill that dictionary
...

//get list from a dictionary by list id
public List getList(string listId) 
{
    return dictionary[listId];
}

Upvotes: 2

Related Questions