Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Basics If Statements "else if" and "else"

Getting errors when running code from the video "else if and else"

Hi, I'm getting the following errors when running code from the teacher's notes/video.

Video link

Snapshot of my Workspace here: link

Here are the errors I'm getting:

treehouse:~/workspace$ dotnet run
CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/home/treehouse/workspace/workspace.c sproj]
Program.cs(9,20): error CS0117: 'Console' does not contain a defini tion for 'Readline' [/home/treehouse/workspace/workspace.csproj]

Simon Coates
Simon Coates
8,177 Points

FYI, these particular error messages are fairly common. If you run a search on the error message (eg. "Program does not contain a static 'Main' method suitable for an entry point"), you can typically find someone who's made a related mistake. And microsoft provides explanation for those error codes. Admittedly, microsoft's explanations aren't necessarily beginner friendly, but their documentation for Compiler Error CS1022 would have directed your attention to braces ({}) and CS1022 explains the missing main method. The explanation for CS0117 they provide is less helpful.

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Ra Bha! As Simon Coates pointed out, your code is missing the Main method. This is a requirement for the program to run at all. It is how the program knows where to begin execution.

Inside the Program class, you were meant to have this Main method:

 static void Main()
    {
        Console.WriteLine("Welcome to the cat food store!");

        string entry = Ask("How many cans are you ordering?");
        int number = int.Parse(entry);
        double total = Price(number);
        Console.WriteLine($"For {number} cans, your total is: ${total}");
    }
}

It is shown being coded around 0:13 of the video. But for some reason, it's missing entirely in your code.

Also, you have a syntax error on line 9 where you typed Readline instead of ReadLine. These are case-sensitive. In fact, you should consider everything case-sensitive in programming including file names and folder :smiley:

Hope this helps! :sparkles:

Hi Jennifer, thanks for pointing out these oversights on my part. I added the code snippet above including the Main method inside the Program class as above. I also corrected the syntax error. Upon running the updated code, I got the following error:

Program.cs(40,1): error CS1022: Type or namespace definition, or end-of-file e xpected [/home/treehouse/workspace/workspace.csproj]

The build failed. Please fix the build errors and run again.

Here's the link to my Workspace Snapshot:link

Is there something else I may have missed?

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi again, Ra Bha! I just forked your new workspace and yes, there's something not quite correct. On line 15 you have an extra } which is closing out/ending the Program class before you're intending. Remove the closing curly brace on line 15 and that should do the trick :smiley:

Simon Coates
Simon Coates
8,177 Points

For the program to run, it needs to know how to run. So there's a convention that you need a static main method. For example, here's a c# class from an online repl.

using System;

class MainClass {
  public static void Main (string[] args) {
    Console.WriteLine ("Hello World");
  }
}

Note the presence of the main method. You can omit the string[] args bit if you want. I'd assume there would be a similar method in any code that treehouse shows running. So it's maybe something you could review a video and see it working.

(The other problem is that Readline should be ReadLine. I missed this first time and didn't refresh my browser. As such, I didn't know that it had already been addressed.)

Thank you, Simon! This is really helpful. As Jennifer points out below, I totally missed this.