2

Have searched in other questions but haven't found how to do in c#. Tried using the Map function, but it's not availabe in c#.

I want to use a for loop to refer to values in objects in StrainsManager Class:

public StrainsManager Ba2;
public StrainsManager Ba3;
public StrainsManager Ba4;
public StrainsManager Ba5;
public StrainsManager Ba6;
public StrainsManager Ba7;
public StrainsManager Ba8;

void Update()
{

    for (int i = 2; i < 9; i++)
    {
        string strainString = "Ba" + i;

        PlayerPrefs.SetFloat(strainString + "Cost", strainString.cost);
        PlayerPrefs.SetFloat(strainString + "IPSAdd", strainString.IPSAdd);
        PlayerPrefs.SetFloat(strainString + "Time", strainString.time);
        PlayerPrefs.SetFloat(strainString + "CostMod", strainString.costMod);
        PlayerPrefs.SetFloat(strainString + "InfectMod", strainString.infectMod);
    }

}

Without the for loop I would have to type in 5 lines per object:

    void Update()
{
    PlayerPrefs.SetFloat("Ba2Cost", Ba2.cost);
    PlayerPrefs.SetFloat("Ba2IPSAdd", Ba2.IPSAdd);
    PlayerPrefs.SetFloat("Ba2Time", Ba2.time);
    PlayerPrefs.SetFloat("Ba2CostMod", Ba2.costMod);
    PlayerPrefs.SetFloat("Ba2InfectMod", Ba2.infectMod);
}

Obviously the second part in the first code does not work(strainString.cost) because strainString is a string and not the object of class StrainsManager. Is the above for loop possible or do I have to do it manually typing 35 lines for those 7 objects?

3
  • 4
    You could declare a List<StrainsManager> or StrainsManager[] instead of declaring them explicitly. Then just loop over the list/array. Commented Feb 25, 2019 at 17:50
  • 2
    If you ever find yourself numbering variables (e.g. Ba2, Ba3, etc.) then you are probably doing it wrong. You should probably be using an array, list, or other data structure to hold all of those variables. It is much easier to deal with Ba[n] because you can substitute a variable for n. Commented Feb 25, 2019 at 17:56
  • 1
    I would also add that it's important to choose meaningful variable names. I don't know the context of your program, but Ba2 wouldn't really mean anything to me if I had to jump onto your team and maintain this code. Sure, intellisense helps to an extent, but that's not good enough IMO. Commented Feb 25, 2019 at 18:07

3 Answers 3

5

You can maintain a Dictionary<string, StrainsManager> to map the keys "Ba#" to your StrainsManager instances.

void Update()
{
    for (int i = 2; i < 9; i++)
    {
        string strainString = "Ba" + i;

        map[strainString].Cost = strainString.cost; //follow this pattern for the other properties.
        //PlayerPrefs.SetFloat(strainString + "IPSAdd", strainString.IPSAdd);
        //PlayerPrefs.SetFloat(strainString + "Time", strainString.time);
        //PlayerPrefs.SetFloat(strainString + "CostMod", strainString.costMod);
        //PlayerPrefs.SetFloat(strainString + "InfectMod", strainString.infectMod);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Just asking: if strainString is of string type, where does the ‘cost’ field come from?
I am providing the incomplete example as a reference to @Mohammad Nadeem as a comparison to his own code example to see how to make the changes.
2

You could declare a List<StrainsManager> or StrainsManager[] instead of declaring them explicitly. Then just loop over the list/array, accessing each item in turn.

In my experience, this is a better long-term strategy than referencing variable names via string. If you (or someone else touching the code) decides to change the way these are named, you're then set up for all sorts of trouble and it's not going to be fun to track down.

public StrainsManager[] StrainsManagerArray = new StrainsManager[7];

/*
 *  Wherever you're setting Ba2-Ba8, instead set StrainsManagerArray[0-6]
 */

void Update()
{

    foreach (StrainsManager strainsManager in StrainsManagerArray)
    {
        //Access each StrainsManager here
    }

}

I left out the PlayerPrefs.SetFloat implementations on purpose, since I'm not 100% sure how that is working (although I have a fair guess). Hopefully it's pretty straightforward where to go from here though.

Comments

1

Try this code snippet:

void Update()
{
    var items = new Dictionary<string, StrainsManager>
    { 
         { nameof(Ba2), Ba2 },
         { nameof(Ba3), Ba3 },
         { nameof(Ba8), Ba8 },
    };

    foreach (var item in items)
    {
        PlayerPrefs.SetFloat($"{item.Key}Cost", item.Value.cost);
        PlayerPrefs.SetFloat($"{item.Key}IPSAdd", item.Value.IPSAdd);
        PlayerPrefs.SetFloat($"{item.Key}Time", item.Value.time);
        PlayerPrefs.SetFloat($"{item.Key}CostMod", item.Value.costMod);
        PlayerPrefs.SetFloat($"{item.Key}InfectMod", item.Value.infectMod);
    }
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.