-2

I am getting "cannot implicitly convert string to char" error. I am trying the following code, how should I specify the cast in the case statement for the <= operator. any help/direction is appreciated. Thanks!

char test1;
    switch(test1)
    {
       case '+':
        //do something
        break;

      case '-' : case '*' : case '<=' :
       //method1
       break

    method 1:

    private void method1(char test2)

    {
      if(test2 == '+' || test2 == '*' || test2.ToString() == "<=")
       {
         token.Add(test2.ToString());
       }
    }

ok, I have a method1 like this:

private void Method1(char test2)
{
  if(test2 == '+' || test2 == '-' || test2 == '/' || test2 == '*')
    {
       //code
       tokens.add(test2);
    }

char test1;
        switch(test1)
        {
           case '+': case '-' : case '*': case '/':
            Method1(test1);
            break;

I am trying to get the tokens if there is an expression like (a+b)*(a-b). tokens is a list of string here. but, what I am also trying to do here is also check for logical operators if they exist in the expression...ex: (a+b)<=5, in this case, I want to check if the next token is <=, if so add it to the list of tokens, the goal is to have one method to handle all the operators(+,-, *,/,<=,>=, ==, !=) and call it in the switch case statement.

5
  • 1
    Fix your code. This code wouldn't compile the way it is. Commented Jun 2, 2015 at 23:17
  • 1
    If you have one char, how can that ever be two chars? The check does not make sense in the first place. What are you really after? Commented Jun 2, 2015 at 23:18
  • By the way, how do you expect test2.ToString() to be equal to "<="? How can one character be equal to two characters? Commented Jun 2, 2015 at 23:18
  • 1
    In your switch statement you have a case with '<=', there are two characters, which is invalid (it is not a valid "char"). Commented Jun 2, 2015 at 23:18
  • You probably need to work with string everywhere, instead of char Commented Jun 2, 2015 at 23:22

2 Answers 2

1

You can use an actionDictionary to do that instead of switch case statement:

private void Test(string operant)
{
    Dictionary<string, Action> actionMap = new Dictionary<string, Action>();
    // map operant to action methods
    actionMap.Add("+", new Action(AddToken));
    actionMap.Add("-", new Action(AddToken));
    actionMap.Add("*", new Action(AddToken));
    actionMap.Add(">=", () =>
    {
        // anynomous method
        token.Add(">=");
    });
    actionMap.Add("/", new Action(Divide));
    actionMap.Add("<=", new Action(LessThanOrEqual));

    // list keep continue here


    foreach (string key in actionMap.Keys)
    {
        if (key == operant)
        {
            actionMap[key]();
        }

    }
}

private void AddToken()
{
}

private void Divide()
{
}

private void LessThanOrEqual()
{
}

// .. and so on
Sign up to request clarification or add additional context in comments.

Comments

0

You're trying use a String (kind of) in a switch statement ('<=').

I'd use a map from a string to a function that "do[es] something". I've found trying to force use a switch causes more problems than it solves.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.