How to use switch-case with strings in c#?

I'm trying to make a really basic calculator program. I am getting the following error message:

Cannot implicitly convert type 'bool' to 'string'

Here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _2019_03_21
{
    class Program
    {
        private static double Negyzet(int alap, int kitevo)
        {
            Console.WriteLine("Kérem a hatvány alapját!");
            alap = int.Parse(Console.ReadLine());
            Console.WriteLine("Kérem a hatvány kitevojet!");
            kitevo = int.Parse(Console.ReadLine());
            return Math.Pow(alap, kitevo);
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Kérem adja meg milyen műveletet szeretne elvégezni!\n\n+ összeadás\n- kivonás\n* szorzás\n/ osztás\n^hatványozás\n\nVálasztott művelet:");
            string muvelet = Console.ReadLine();
            switch (muvelet)
            {
                case (muvelet == "^"): Console.WriteLine("A hatvány értéke:     {0}", Negyzet(0, 0)); break;
                default: break;
            }
            Console.ReadKey();
        }
    }
}

Upvotes: 0

Views: 3585

Answers (3)

valerian Havaux
valerian Havaux

Reputation: 111

Until C# 6, the switch instruction was reserved for primitive types. You can now switch to patterns.

Pattern Matching

And so you can do that kind of thing :

class Program
{
    static void Main(string[] args)
    {
        Print("+", 2, 2);
        Print("-", 2, 2);
        Print("/", 2, 2);
        Print("*", 2, 2);
        Print("^", 2, 2);
        Print("%", 2, 2);
        Print(" ", 2, 2);
        Print("", 2, 2);
        Console.Read();
    }
    static void Print(string op, int nmb1, int nmb2)
    {
        var res = Compute(op, nmb1, nmb2);
        Console.WriteLine(res != null ?
            $"{nmb1} {op} {nmb2} = {res}" :
            $"invalid {op?.Trim()} operator description");
    }

    static int? Compute(string op,int nmb1,int nmb2)
    {
        switch (op)
        {
            case "+":
                return nmb1 + nmb2;
            case "-":
                return nmb1 - nmb2;
            case "*":
                return nmb1 * nmb2;
            case "/":
                return nmb1 / nmb2;
            case "%":
                return nmb1 % nmb2;
            case "^":
                return nmb1 ^ nmb2;
            case var o when (o?.Trim().Length ?? 0) == 0:
                // white space
                return null;
            default:
                return null;
        }
    }
}

console output :

2 + 2 = 4
2 - 2 = 0
2 / 2 = 1
2 * 2 = 4
2 ^ 2 = 0
2 % 2 = 0
invalid operator 
invalid operator 

Upvotes: 0

Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34150

muvelet is a string while muvelet == "^" is a comarision which is boolean (it is either true or false

switch(muvelet)  
{
     case "^":
         // code for when it is equal to "^"
          break;
    //other cases
    default:
         //it is unknown character
}

note that the type in your switch (that is a string in this case) should match the type of cases

Upvotes: 3

SBS
SBS

Reputation: 836

You're using the case clause in a wrong way. It expects integer or String values - but you supply a Boolean value. It's easy to fix that, however. Just write the case clause like that:

case "^":

Then it should compile and work as expected.

Upvotes: 3

Related Questions