Search code examples
c#list

C# Using Variable To Switch Between Two Lists


This may be a silly question, but here is my scenario: I have two lists, one containing hockey player objects and another containing baseball player objects.

public static List<BaseballPlayer> Baseball List = new List<BaseballPlayer>();
public static List<HockeyPlayer> Hockey List = new List<HockeyPlayer>();

I have one screen that draws more details on a player object if a button is pushed (the below is an example of hundreds of lines drawn):

GUI.Label(new Rect(x, y, 130, 32),Lists.BaseballPlayer[i].accessPlayerName);

However, is there a way to dynamically control which list name is used. So something like:

string one;

And then have the draw call do something like this:

GUI.Label(new Rect(x, y, 130, 32),Lists.one[i].accessPlayerName);

Where string one can be defined as either "BaseballPlayers" or "HockeyPlayers".

I don't want to have to duplicate two screens, one showing the BaseballPlayer list and one showing the HockeyPlayer list. If there were a way to dynamically insert the list name, that would be great.

Thanks!


Solution

  • Well, a simple if inline can be enough.

    Suppose you use a boolean to determine which list use:

    bool useHockey = true; //control this variable anyway you like, a property, a function, anything you want
    

    Then you can do:

    GUI.Label(new Rect(x, y, 130, 32), (useHockey ? Lists.Hockey : Lists.Baseball)[i].accessPlayerName);