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]);
}
19
  • Can you show how BRData is declared and what's inside of it? Commented May 31, 2018 at 4:02
  • Your code is confusing. Which type is BRData? Are you confusing method OnBeadIncreased with an enum? Please claify. Commented May 31, 2018 at 4:04
  • 1
    @TheGinxx009 Yes. Edit your question to be ansewarble and I will remove the downvote. And try to answer. Commented May 31, 2018 at 4:29
  • 1
    This shows how, but you will need your enum names to match the strings. So you can't parse B as BLUE, for example. Commented May 31, 2018 at 4:39
  • 1
    @john Nice . It was really helpful . Commented May 31, 2018 at 4:44

2 Answers 2

3

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))
    {

    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

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
Please add EDIT to your question followed by your new code as it is. Do not modify it in any where.
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.

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.