• Advertisement
Sign in to follow this  

C# Exercise based off previous C++ Workshop

This topic is 3943 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone, In my anticipation of the upcoming C# workshop, I decided to study ahead a bit. During the course of my readings, I thought back to the C++ Workshop the jwalsh had put together, and decided to try and "port" some of the examples into C#. Rather than using the console, I used a windows form to accomplish the desired result. This is a beginner level exercise. You can use the console if you wish, though, I am not sure how to do it that way, maybe someone else can show how to do that. Anyhow, here is the exercise as listed by jwalsh:
Quote:
2. Color Menu: In this exercise you are going to write a program that shows the user a menu asking them what color they would like to display their menu in. The menu itself, will be a list of matching numbers and colors. Use the menu below to determine menu options. Present the menu to the user over and over again, allowing them to change the color of the menu until they choose the q option. Once they select ‘q’, terminate the program. There is helper code below to allow you to change the color of the console window. Once the user has selected a color option, set the color for the console window, and then re-print the menu onto the screen. Although not strictly necessary for this exercise, I encourage you to make an enum out of the following menu options and color names, and then use a switch statement to check for color values. The program will work just fine without, however….perhaps try it both ways and see how it’s different in this case. Show the users the following menu: -------------------------------------------- 1. Dark Blue 2. Dark Green 3. Teal 4. Burgundy 5. Violet 6. Gold 7. Silver 8. Gray 9. Blue 10. Green 11. Cyan 12. Red 13. Purple 14. Yellow 15. White Q. Quit
Now, I did not use the console, as stated earlier. Try to implement this example by whatever means you can. I will say I took a bit different approach and listed each color in a listbox, and changed text color accordingly. Hope jwalsh doesnt mind me posting this, and hope it gives a challenge to any other new aspiring programmers out there in the C# world. Cya, Shawn

Share this post


Link to post
Share on other sites
Advertisement
shawnre,

I dont mind. But I will be using many of the exercises from the C++ workshop in the C# workshop, since the projects will be very similar.

So you can get a head-start now, or wait until the exercises are assigned.

Doesn't matter to me. [smile]

Cheers!

Share this post


Link to post
Share on other sites
I decided to play around with this and came up with two solutions.

The first solution uses a switch, but not a new enum. System.ConsoleColor matches up nicely. It's the better of the two solutions.

The second solution is, well, interesting. I had some fun with the second one. It's not nearly as easy to follow so if there's any questions just ask.


using System;

namespace ConsoleColorMenu
{
class ConsoleColorMenu
{
static void Main(string[] args)
{
string fmtStr = "{0,2}. {1}";
string inpStr;
int inpInt;

do
{
Console.Clear();
Console.WriteLine(fmtStr, "1", "Dark Blue");
Console.WriteLine(fmtStr, "2", "Dark Green");
Console.WriteLine(fmtStr, "3", "Teal");
Console.WriteLine(fmtStr, "4", "Burgundy");
Console.WriteLine(fmtStr, "5", "Violet");
Console.WriteLine(fmtStr, "6", "Gold");
Console.WriteLine(fmtStr, "7", "Silver");
Console.WriteLine(fmtStr, "8", "Gray");
Console.WriteLine(fmtStr, "9", "Blue");
Console.WriteLine(fmtStr, "10", "Green");
Console.WriteLine(fmtStr, "11", "Cyan");
Console.WriteLine(fmtStr, "12", "Red");
Console.WriteLine(fmtStr, "13", "Purple");
Console.WriteLine(fmtStr, "14", "Yellow");
Console.WriteLine(fmtStr, "15", "White");
Console.WriteLine(fmtStr, "Q", "Quit");
Console.Write("Select Option: ");
inpStr = Console.ReadLine().Trim();
if (int.TryParse(inpStr, out inpInt))
{
switch (inpInt)
{

case 1: Console.ForegroundColor = ConsoleColor.DarkBlue; break;
case 2: Console.ForegroundColor = ConsoleColor.DarkGreen; break;
case 3: Console.ForegroundColor = ConsoleColor.DarkCyan; break;
case 4: Console.ForegroundColor = ConsoleColor.DarkRed; break;
case 5: Console.ForegroundColor = ConsoleColor.DarkMagenta; break;
case 6: Console.ForegroundColor = ConsoleColor.DarkYellow; break;
case 7: Console.ForegroundColor = ConsoleColor.DarkGray; break;
case 8: Console.ForegroundColor = ConsoleColor.Gray; break;
case 9: Console.ForegroundColor = ConsoleColor.Blue; break;
case 10: Console.ForegroundColor = ConsoleColor.Green; break;
case 11: Console.ForegroundColor = ConsoleColor.Cyan; break;
case 12: Console.ForegroundColor = ConsoleColor.Red; break;
case 13: Console.ForegroundColor = ConsoleColor.Magenta; break;
case 14: Console.ForegroundColor = ConsoleColor.Yellow; break;
case 15: Console.ForegroundColor = ConsoleColor.White; break;
}
}
} while (inpStr.ToUpper() != "Q");
}
}
}





using System;

namespace ConsoleColorMenu
{
class Program
{
static void Main(string[] args)
{
Array colorVals = Enum.GetValues(typeof(ConsoleColor)); ;
string fmtStr = "{0,2}. {1}";
string inpStr;
int inpInt;
do
{
Console.Clear();
foreach (object cv in colorVals)
{
Console.WriteLine(fmtStr, (int) cv, cv);
}
Console.WriteLine(fmtStr, "Q", "Quit");
Console.Write("\nSelect Color: ");
inpStr = Console.ReadLine().Trim();
if (Int32.TryParse(inpStr, out inpInt))
{
foreach (object cv in colorVals)
{
if ((int) cv == inpInt)
{
Console.ForegroundColor = (ConsoleColor) cv;
}
}
}

} while (inpStr.ToUpper() != "Q");
}
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by brandogon
The second solution is, well, interesting. I had some fun with the second one. It's not nearly as easy to follow


But it is, basically, what you should be doing, *not* the first way. The second way is more maintainable; you won't need to change things if the enum contents change. Please keep in mind that "explicit" does not equal "more readable".

But I'd like to offer a couple suggestions:

- Instead of using the enumeration values, calculate values to display as the numbers from 1 upward, and then use the provided value as an array index.

- Scope your variables closely, except to avoid expensive calculations.

- Store ConsoleColors in an array of ConsoleColor to avoid later casting.


using System;

namespace ConsoleColorMenu
{
class Program
{
static void Main(string[] args)
{
// I don't know if there's any casting required here? I do know
// from looking at docs that things like
// "foreach (ConsoleColor cc in Enum.GetValues(typeof(ConsoleColor)))"
// will work, so I assume this does too...
ConsoleColor[] colorVals = Enum.GetValues(typeof(ConsoleColor));
do
{
string formatString = "{0,2}. {1}";
Console.Clear();

int position = 1;
foreach (ConsoleColor cv in colorVals)
{
Console.WriteLine(formatString, position, cv);
position += 1;
}
Console.WriteLine(formatString, "Q", "Quit");

Console.Write("\nSelect Color: ");
int selection;
if (Int32.TryParse(Console.ReadLine().Trim(), out selection)
&& selection >= 1 && selection <= colorVals.length)
{
Console.ForegroundColor = colorVals[selection - 1];
}
} while (inpStr.ToUpper() != "Q");
}
}
}



Of course, I don't actually really know C# so I don't know what's idiomatic there. Just going with my instinct from general programming knowledge :)

Share this post


Link to post
Share on other sites
Yet another solution; slightly divergent from the exercise. It also has an... assumption which is poor practice.


using System;
using System.Collections.Generic;
using System.Text;

namespace ColorPicker {
class Program {
static void Main(string[] args) {
while (true) {
foreach (ConsoleColor color in Enum.GetValues(typeof(ConsoleColor))) {
Console.ForegroundColor = color;
Console.WriteLine("{0}. {1}", (int)color, color);
}
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("q. Quit");

string input = Console.ReadLine();
input=input.Trim();

if (input == "q" || input == "Q") {
return;
}

try {
ConsoleColor inputColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), input, true);
Console.Write("Thank you for selecting: ");
Console.ForegroundColor = inputColor;
Console.Write("{0}", inputColor);
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(".");

} catch (Exception reason) {
Console.WriteLine("I'm sorry, I don't understand \"{0}\"", input);
}

Console.WriteLine();
}
}
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
But it is, basically, what you should be doing, *not* the first way. The second way is more maintainable; you won't need to change things if the enum contents change. Please keep in mind that "explicit" does not equal "more readable".


Interesting point. I'll keep that in mind.

Quote:
Original post by Zahlman
But I'd like to offer a couple suggestions:

- Instead of using the enumeration values, calculate values to display as the numbers from 1 upward, and then use the provided value as an array index.

- Scope your variables closely, except to avoid expensive calculations.

- Store ConsoleColors in an array of ConsoleColor to avoid later casting.


The first suggestion would also allow for a sequential numbered list even if, for some odd reason, the enum was non-sequential. I'll try that out along with avoiding the casting.

formatString is only used inside the loop, so it makes sense to move it there. :)


Quote:
Original post by Telastyn
Yet another solution; slightly divergent from the exercise.

That seems to be a much better coloring style for picking a color from a menu.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement