handover ConsoleColor to method in C# - c#

I want to write a short method that I can use to write one single line in the console colorful.
Currently I have to call my method like this:
ChangeConsoleColor("ABC", ConsoleColor.Yellow, ConsoleColor.Red);
But I want to be able to call it like this:
ChangeConsoleColor("ABC", Yellow, Red);
The perfectly working code I have looks like this:
static void Main(string[] args)
{
string myText = "ABCDEFG";
ChangeConsoleColor(myText, ConsoleColor.Yellow, ConsoleColor.Red);
System.Threading.Thread.Sleep(6000);
}
private static void ChangeConsoleColor(string Text, ConsoleColor BGC, ConsoleColor FGC)
{
Console.BackgroundColor = BGC;
Console.ForegroundColor = FGC;
Console.WriteLine(Text);
Console.ResetColor();
}
How do I need to change my code to get rid of the 'ConsoleColor' in the function call part?

Related

How can I have a dynamic property in c#

I am creating a method that allows me to print a message in the console and to be able to change the color of the message in that same method.
I send 2 parameters to carry out this action, message (which is the message that is printed in the console) and color (which is the color that I want to be printed in the console)
I have my code like this:
public void Write(string message, string color = "White"){
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(message);
}
But I need ConsoleColor.White to grab my dynamic color as follows (similar to javascript): ConsoleColor [color];
How can I achieve this? Thanks in advance.
What I tried was the above: ConsoleColor[color];
ConsoleColor is an Enum, so the usual way to do this would be to take a ConsoleColor as the second parameter:
public void Write(string message, ConsoleColor color = ConsoleColor.White){
Console.ForegroundColor = color;
Console.WriteLine(message);
}
If you really need it to be a string, you can use Enum.Parse, but this runs the risk of a failed conversion if the caller passes an invalid color value:
public void Write(string message, string color = "White"){
Console.ForegroundColor = Enum.Parse<ConsoleColor>(color);
Console.WriteLine(message);
}
Or if you want a version that won't throw with a bad value:
public void Write(string message, string color = "White"){
Console.ForegroundColor = Enum.TryParse<ConsoleColor>(color, out ConsoleColor parsedColor)
? parsedColor : ConsoleColor.White;
Console.WriteLine(message);
}

How to pass color as parameter in c#?

I need to change font color many times in console app.
Instead of typing (or copying) each time :
Console.ForegroundColor = ConsoleColor.MyColor; in my code,
I want to type only
c(Red) or c(Yellow).
I thaough about something like this:
static void c(<???> myColor){
Console.ForegroundColor = ConsoleColor.MyColor;
}
How can I achieve that ?
Here is a method you can use to set the foreground color of your Console. I named the function as SetConsoleForeground but you can set it as you like such as c
/// <summary>
/// Sets Console Foreground color to the given color
/// </summary>
/// <param name="consoleColor">Foreground color to set</param>
private static void SetConsoleForeground Color(ConsoleColor consoleColor) {
Console.ForegroundColor = consoleColor;
}
Instead of passing c(Red) pass c(ConsoleColor.Red) and use parameter of type ConsoleColor enum in defination of method c()
public static void c(ConsoleColor myColor){
Console.ForegroundColor = myColor;
}
Call function using
c(ConsoleColor.Red);
MSDN ConsoleColor
I want to type only c(Red) or c(Yellow)
If you want to pass string called Red or Yellow and assign ForegroundColor to your console, then you can try below
public static void SetForegroundColor(string colorName)
{
//Set black as foreground color if TryParse fails to parse color string.
if(Enum.TryParse(ConsoleColor, colorName, out ConsoleColor color)
Console.ForegroundColor = color;
else
Console.ForegroundColor = ConsoleColor.Black;
}
Now you can pass color name as a string to this function, Like
SetForegroundColor("Red");
SetForegroundColor("Yello");
MSDN: Enum.TryPrase

Storing a colour in a variable for future use

I am writing a bit of a dungeon crawler as I learn my way through c#. I am using visual studio and the application is a "Console App (.NET Framework)". My problem is that I have an option in the main menu to change text colour and I am trying to find away to make this change all the other times I change the colour so that it returns to a set default.
I have tried to assign the colour to a variable, lets say, default and then do the Console.ForegroundColor = ConsoleColor.default; I looked online everywhere and can't find a fix.
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("example");
Console.ForgroundColor = Console.Color.White;
// This needs to be default ^^
You can create a property that will of ConsoleColor which will considered as a default color.
Just Assign it when ever you want default color should set to Console.ForegroundColor
Something like,
public class Program
{
public static ConsoleColor DefaultColor { get; set; } = ConsoleColor.Black;
public static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("example");
//You can use DefaultColor whenever you want to assign default color to Foreground
Console.ForegroundColor = DefaultColor;
}
}

First time attempting programing. What am I doing wrong here?

enter image description hereFor my first try, I looked up some YouTube tutorials. One is showing how to create a Magic 8 Ball. I am following the instructions pretty much exactly but it won't work. When written like this it's fine:
namespace Magic8Ball
{
class Program
{
static void Main(string[] args)
{
//Preserve Current Console Text Color
ConsoleColor OldColor = Console.ForegroundColor;
//Change Console Text Color
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Magic 8 Ball. By: Conner Bostock");
Console.ForegroundColor = ConsoleColor.White;
Console.Write("Ask a question?: ");
Console.ForegroundColor = ConsoleColor.DarkGray;
String QuestionString = Console.ReadLine();
//Cleaning Up
Console.ForegroundColor = OldColor;
}
String Void TellPeopleTheName(String Text);
}
}
As you see I am trying to try create a String, not that I need one to do this. I just want to see how it works (So when I need it I don't need to re-type it and can just post this) But starting off it says "TellPeopleTheName must declare a body" I'm confused as this did not happen in the video. Not only that but when I cut and paste the code to join with it. For some reason it all breaks and no longer works:
namespace Magic8Ball
{
class Program
{
static void Main(string[] args)
{
//Preserve Current Console Text Color
ConsoleColor OldColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.White;
Console.Write("Ask a question?: ");
Console.ForegroundColor = ConsoleColor.DarkGray;
String QuestionString = Console.ReadLine();
//Cleaning Up
Console.ForegroundColor = OldColor;
}
String Void TellPeopleTheName(String Text);
//Change Console Text Color
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Magic 8 Ball. By: Conner Bostock");
The entire bottom section is underlined in red saying "Invalid Token" and Writeline does not exist in this context". This confuses me more as this did not happen in the video either. What am I doing so wrong?
Thanks for the help and sorry for the nooby comments. I'm 20 and always wanted to learn coding but always been too busy working. Recently broke my back and now have plenty of time sitting doing nothing so I figured I would give it a shot.
This is mine (on top) and his (Bellow) and even after your help (I did go and re-watch and noticed I put String Void and not Static void) These problems still are here and I still don't understand. I apologise. I am really trying to learn and understand. This is a section I could skip and still make the 8-ball but I want to know why it does not work and how to fix it so then I know for when I do need to use this.
NEW FULL CODE:
namespace Magic8Ball
{
class Program
{
static void Main(string[] args)
{
//Preserve Current Console Text Color
ConsoleColor OldColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.White;
Console.Write("Ask a question?: ");
Console.ForegroundColor = ConsoleColor.DarkGray;
String QuestionString = Console.ReadLine();
//Cleaning Up
Console.ForegroundColor = OldColor;
}
//This will tell people the name
static void TellPeopleTheName()
//Change Console Text Color
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Magic 8 Ball. By: Conner Bostock");
}
This line is out of place and doesn't actually do anything:
String Void TellPeopleTheName(String Text);
It's void, not Void. Casing matters.
You're giving it two return types, String and void. It either returns something or it doesn't.
That's a method header, but you're missing a method body. As a single line of code it doesn't make sense. Methods need to do something.
For example, a method might look like this:
void TellPeopleTheName(string text)
{
// write code in here to do something
}
Then you would invoke (call) that method somewhere from another method, when you want to perform that operation:
TellPeopleTheName("some text");
Or, in the case of a method which returns a string:
string TellPeopleTheName(string text)
{
// code which does something
return "some string";
}
Invoking it would be the same, but you could store the return value in a variable or otherwise use it in some way:
var someVariable = TellPeopleTheName("some text");
(Note: There are an ongoing variety of issues you may encounter even in trying what's described here. The difference between a static and non-static method come to mind, particularly if you try to call this from main(). Or the variety of ways you may return from a method or use the returned value. And so on. There's much to learn. Good luck!)
String Void TellPeopleTheName(String Text);
should be
void TellPeopleTheName(String Text)
{
}
That would take care of "missing body" error!
void should be in lowercase.
You are missing open/close brackets on the method too.
I notice a couple things you've missed in understanding.
What you've tried to do is write a method to write to the console the name of the program, but the method is not structured correctly.
The general structure of a method is...
AccessModifier static/non-static ReturnType MethodName (ParameterType ParameterName) {
//code to do something
}
An access modifier defines how accessible the method is to other code (as a starting programmer, just keep it as public until you become more advanced).
Putting static or nothing (non-static) is a little confusing for a beginner, if you'll be calling the method from Main (a static method) then it will need to be static, that's sufficient enough for a beginner's understanding for now.
The return type defines what the method should return back when the method is called.
The method name is just how the method is called, should be descriptive and clear what the method does.
A parameter type is the type of object that is being passed as a parameter for the method to use.
A parameter name is just a name, the name should easily explain what the parameter's purpose is for the method.
Following this structure, your method should look like...
public static void TellPeopleTheName(string text)
{
//Change Console Text Color
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Magic 8 Ball. By: Conner Bostock");
}
If you want to make the method more "correct", then it would be...
public static void TellPeopleTheName(string nameOfProgram)
{
//Change Console Text Color
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(nameOfProgram);
}
and you would call the method like this...
TellPeopleTheName("Magic 8 Ball. By: Conner Bostock");

C# - Rewrite/edit line while program is running

Is there any way that i can edit/rewrite certain lines that have already bin printed by the Console.PrintLine() method? I have to be able to edit any line that is shown in the prompt.
This is an example on what the code that i'm trying to get running, maybe can look like:
public static void RewriteLine(LineNr, Text)
{
//Code
}
Console.WriteLine("Text to be rewritten");
Console.Writeline("Just some text");
RewriteLine(1, "New text");
Example to show which line that i want rewritten based on output from the previous code:
Text to be rewritten //This line (has already bin executed by the Console.WriteLine() method) shall be replaced by: "New text"
Just some text
It should look like this:
public static void RewriteLine(int lineNumber, String newText)
{
int currentLineCursor = Console.CursorTop;
Console.SetCursorPosition(0, currentLineCursor - lineNumber);
Console.Write(newText); Console.WriteLine(new string(' ', Console.WindowWidth - newText.Length));
Console.SetCursorPosition(0, currentLineCursor);
}
static void Main(string[] args)
{
Console.WriteLine("Text to be rewritten");
Console.WriteLine("Just some text");
RewriteLine(2, "New text");
}
What's happening is that you change cursor position and write there something. You should add some code for handling long strings.

Categories

Resources