Link to home
Start Free TrialLog in
Avatar of December2000
December2000

asked on

Switch Statements in C# Method

I am getting errors that o and p doesnt exist in the current context and Cannot implicitly convert type 'double' to 'string'... what am I doing wrong?

 {
        double cost;
        string wood;
        
        Console.WriteLine("What type of wood ?  ");
        wood = Console.ReadLine();
        wood = Convert.ToDouble(cost);

        switch (wood)
        {
            case o:
                cost = 150;
                break;
            case p:
                cost = 140;
                break;
            default:
                cost = 180;
                break;
        }

Open in new window

Avatar of Haver Ramirez
Haver Ramirez

Cost already are double why you try to convert again instead of convert to STRING, also you are using wood to save the readline result, why you pass the cost again?
These are both pretty obvious if you read the code carefully.

wood is declared as string. You need to compare to a string.

case "o":
case "p":

As for the conversion error, you're doing a Convert.ToDouble and trying to assign it to a string variable.
Avatar of December2000

ASKER

@CraigWagner if you got to be mean don't respond.... if I knew I would not ask and you would not get points... you had to learn too so get off your soap box dude.... If the question is so obvious that it irritates you....pass to something at your "level" :/
ASKER CERTIFIED SOLUTION
Avatar of Dmitry G
Dmitry G
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you @ anarki_jimbel!
Apologies @CraigWagner maybe I misinterpreted your response:(