Assign the name of string as name of predefined List

I am making a Roulette game in Unity 5.0. I have a list in C# where I store the bets the player places. I use the following code to retrieve the bets placed by the user. Bets1 is the list which saves the type of bet placed by the user like SUP0 is straight up bet on Number 0, SPLT1 is split bet on number 1 & 2. These SUP0 and SPLT1 are lists in which these numbers are added.

for (int i = 0; i < Bets1.Count; i++)
    {
        string a = Bets1*.listname;*

Debug.Log(a);
//the output in console is SUP0 for Straight up bet on number 0

for (int j = 0; j < a.Count; j++)
{
Debug.Log(a*);*
}
}
The above code shows error on a.Count and a*. My question is how to assign the value of variable a to be used as name of predefined list.*

You don’t show what is the element type of the Bets1 list, which would help understanding your code.
Anyway, you seem to read the listname member of every element in that list, that is a string, and storing it in a.
Then, for some reason you iterate over every character in a?
First, a.Count fails because a is a string, and strings don’t have the Count method. If you want to know the amount of characters in a string, that is .Length.
Second, the Debug.Log(a*); instruction is wrong. You are using the iteration variable of the outer for loop (i), instead of the iteration variable of the inner for loop (j), so of course, that will lead to some IndexOutOfBoundsException when you try to read a position not contained in the string.*
In general, you need to rethink this, and explain better what you are attempting.

If I understand correctly, what you want to do is something like

Int[] SUP0 = new int[] {0};
Int[] SPLT1 = new int[] {1, 2};
...
string a = Bets1*.listname;* 

// a has the value SPLT1 so you want to get numbers 1 & 2
You could do this using “reflection” and fiddling with variable names, but that’s not the right way to go 95% of the time. Instead use a dictionary.
Dictionary<string, int[]> bets = Dictionary<string, int[]>();
bets.Add(“SUP0”, new int[]{0});
bets.Add(“SPLT1”, new int[]{1, 2});

string a = Bets1*.listname;*
// a has the value SPLT1 so you want to get numbers 1 & 2
int[] numbers = bets[a];

A dictionary is a collection where you can Add() key-value pairs and fetch those values later using a key, your bet name in this case.
Looking at the rules of roulette, “a bet” is a fairly complex thing and i dont think this is the best way to represent its properties. This way for example you need to make a separate int list with a separate id of every straight up bet even though all of them act just the same.
You are not asking butmy first instinct would be to make a Bet class and extend it to get different kinds of bets.
public abstract class Bet {
protected int betOnNumber;
public Bet (int onNumber){
betOnNumber = onNumber;
}
public abstract bool WinsWith(int spinnedNumber);
}

public class StraightUpBet {
public StraightUpBet (int onNumber) : base(onNumber) { }
public override bool WinsWith(int spinnedNumber) {
return spinnedNumber == betOnNumber;
}
}

public class SplitBet {
public SplitBet (int onNumber) : base(onNumber) { }
public override bool WinsWith(int spinnedNumber) {
return spinnedNumber == betOnNumber || spinnedNumber == betOnNumber + 1;
}
}
So each bet knows how to calculate whether it won with the number that was spun. If using inheritance feels out of your scope, it might be better to use your system :slight_smile: