?
Solved

Switch Statements in C# Method

Posted on 2012-09-17
6
Medium Priority
?
334 Views
Last Modified: 2012-09-17
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

0
Comment
Question by:December2000
6 Comments
 
LVL 8

Expert Comment

by:Crashman
ID: 38407313
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?
0
 
LVL 21

Expert Comment

by:Craig Wagner
ID: 38407314
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.
0
 

Author Comment

by:December2000
ID: 38407436
@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" :/
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 2000 total points
ID: 38407438
See the code:
        static void Main(string[] args)
        {

                double cost;
                string wood;
                
                Console.WriteLine("What type of wood ?  ");
                wood = Console.ReadLine();
                //wood = Convert.ToDouble(cost); - you do not need this line, it's a mistake

                switch (wood)
                {
                    case "o":
                        cost = 150;
                        break;
                    case "p":
                        cost = 140;
                        break;
                    default:
                        cost = 180;
                        break;
                }
                Console.WriteLine("Cost = " + cost.ToString());
                Console.ReadLine();
        }

Open in new window

0
 

Author Closing Comment

by:December2000
ID: 38407450
Thank you @ anarki_jimbel!
0
 

Author Comment

by:December2000
ID: 38407549
Apologies @CraigWagner maybe I misinterpreted your response:(
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month13 days, 16 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question