0

How can I compare the string data i use to my enum

I made this enum

public enum BeadColor
{
    BLUE,
    RED,
    GREEN,
    NONE,
}

public BeadColor previousColor = BeadColor.NONE;

and I will create a string array like this

string str = {"P", "B", "T"};

for doing this to an enum

public enum BeadColor
{
    BLUE = "P",
    RED = "B",
    GREEN = "T",
    NONE,
}

So if this is possible i would like to use the enum to check if my string BigRoadData.Contains(/*the enum BLUE for example*/)

cause instead of doing the normal way

if(BigRoadData.Contains(str[0])){
   //print a
}

I would like to do it "IF POSSIBLE" like this

if(BigRoadData.Contains(/*the enum blue for example*/)){
   //print a
}

EDIT

CustomClass.cs

public string BigRoadData = "";
public string[] strData = {"P  ,B  ,B  ,P  ,B  ,B  ,B  ,B  ,B  ,B  ,P  ,P  ,B  ,P  "};
public void MakeBigRoad(string data){

    BigRoadData = data;

    for(int i = 0; i < strData.Length; i++){
        BigRoadData += strData [i];
        BigRoadData += ",";
    }
}

public void ScoreBoardCondition(){

    if(BigRoadData.Contains(....)){

    }
}

MainClass

Here i will call it like this

CustomClass.ScoreBoardCondition();

EDIT

On my CustomClass.cs

public enum _Data
{
    P,
    B,
    T,
}

public enum _BeadColors
{
    BLUE,
    RED,
    NONE,
}

public _Data previousColor = _BeadColors.NONE;

class CustomClass{

}

//extension method for contains
public static bool Contains(this string input, _Data data){
string[] str = { "P", "B", "T" };

if(data == BaccaratScoreboard._Data.P){
    return input.Contains (str [0]);
}

if(data == BaccaratScoreboard._Data.B){
    return input.Contains (str [1]);
}

if(data == BaccaratScoreboard._Data.T){
    return input.Contains (str [2]);
}
Ginxxx
  • 1,602
  • 2
  • 25
  • 54

2 Answers2

2

There are many ways to do this.

1.You can map your Enum to the data in the string array with a Dictionary then use Dictionary.ContainsKey(YourEnum.Value).

2.You make the names of the enum variable the-same with the name of the values in the string array then use Enum.Parse to make this conversion and compare them.

3.You can make an Contains extension method for the string class that hides your BigRoadData.Contains(str[0]). This function will take BeadColor enum as argument and allows you to compare with that instead of str[0].

I will provide an example for the last method. You can implement the other ones yourself if you wish to use them instead.

The enum:

public enum BeadColor
{
    BLUE, //P
    RED,  //B
    GREEN, //T
    NONE,
}

The extension method:

public static class ExensionMethod
{
    public static bool Contains(this string input, BeadColor cColor)
    {
        string[] str = { "P", "B", "T" };

        if (cColor == BeadColor.BLUE)
            return input.Contains(str[0]);

        if (cColor == BeadColor.RED)
            return input.Contains(str[1]);

        if (cColor == BeadColor.GREEN)
            return input.Contains(str[2]);

        return false;
    }
}

The extension method maps "P" to BeadColor.BLUE, "B" to BeadColor.RED and "T" to BeadColor.GREEN and makes it possible to check if the color exists using the BeadColor enum.

Usage:

It works on string

public string BigRoadData = "";

void Start()
{
    if (BigRoadData.Contains(BeadColor.BLUE))
    {

    }
}
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Hey sir ` Unexpected symbol `bool', expecting `class', `delegate', `enum', `interface', `partial', or `struct'` I just tested that out i thought it was working but its not – Ginxxx May 31 '18 at 07:45
  • Please add EDIT to your question followed by your new code as it is. Do not modify it in any where. – Programmer May 31 '18 at 08:09
1

You can't use stringas the type of your enum.
You can try char, but is not recommended.
See this question and answers: https://stackoverflow.com/q/8588384/2025364

By the way, concatenating strings like that is VERY bad: BigRoadData += strData [i];
Use String.Concat (or StringBuilder) instead.

Adam Calvet Bohl
  • 1,009
  • 14
  • 29