C# Course for Absolute Beginners – Lesson 1.S
To review: The homework assignment was to accept user input, test the input, and display a message. There were some extra steps at the end as well. Don’t worry if you were stuck on the homework assignment. Sometimes that happens; this lab will help you work through each of the issues.
Step 1: Coding the Assignment
Begin by creating a new project.
Observe the code we’ve created in the above image.
- In line 12, create a string called userValue and initialize it to an empty string.
- In line 14, create a Console.WriteLine – type in the numbers and press Return.
- In line 15, add the userValue = Console.ReadLine();
- In lines 16 to 20 create an if statement checking the user input. If the user enters the information correctly the program will display the recalibrate message.
-In lines 21 to 24 create the else statement in case the user enters the incorrect data. Notice that we don’t use any else if statements in this program.
Step 2: Testing the Assignment
When we run the program we see that if the user enters the correct values, the program runs the recalibration code.
When the user enters incorrect values the program runs the Failure code.
That was the Day 01 Homework Assignment, and we see that our code is working correctly.
Step 3: Solving the First Challenge
The Day 01 Homework Assignment also offered two challenges. The first challenge was to make the application beep if there was a failure.
We provided a hint to look at the IntelliSense and see what you find. If you scroll through IntelliSense you’ll notice that there’s a ‘Beep’ method. We also see a tip that the Beep method plays the sound of a beep of a specified frequency during the duration through the console speaker.
Add the following code below line 23 (Where our program writes out "Failure!"):
Now when the application is run and incorrect values are entered, our computer system will also beep.
Step 4: Solving the Second Challenge
The second homework challenge was to force the Console Window text to appear in a red font. This task is more tricky because it requires some understanding of a concept called enumerations. You probably had to go to MSDN to solve this. However it’s entirely possible that you were able to solve this on your own.
To solve this, enter the following code before the first Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Red.
Why not simply enter "red?" It doesn’t work that way. "Red" doesn’t have any meaning by itself. It’s not a keyword, but because it’s defined as a property of the class and it returns a computer version of the color red, it knows then what color to use as the display color. Again, this is an enumeration – we’ll cover enumerations much later.
When we run the application, we see that the text is now displayed in red. If incorrect values are entered, a beep will also be played.
If you were able to complete the basic assignment on your own, congratulations – you’re doing very well. If not that’s okay, but be sure to review the labs where those key concepts were discussed. Be sure to understand those before continuing on to the Day 02 lessons. Above all, remember: Today you started programming C#, writing code on your own, and you now understand the basics of writing a C# application. That’s quite a bit of progress.