C #: variable class references

I am trying to use variables in C # but hit the roadblock. Does not work.

public static void SetBG(string s)
{
    Console.BackgroundColor = ConsoleColor.s;
}

      

A very simple method that should set the background color of the console to match what the programmer sent through the variable s

. If I used SetBG("Red");

, it should get through, but it doesn't. This is due to the fact that System.ConsoleColor' does not contain a definition for 's'

.

How do I do this in C #?

+3


source to share


6 answers


This is not the correct way to do it, and it looks like there are two main concepts that you are stuck with, and understanding them will help you get it right.

First, it s

is a variable in your current class. ConsoleColor.s

will refer to a variable s

in the class ConsoleColor

(it doesn't have one, hence your mistake). This is called scope , or encapsulation , and allows us to reuse common words / names without conflicts.

Second, your code will be compiled before it runs, and ConsoleColor.s

(assuming there is such a thing) it won't speak anymore ConsoleColor.s

. Instead, it points to the location in memory where the value representing "s" is stored. When passed s

to your method, there will be a string such as "Red" or "Blue". There is no relationship between such a string and a slot in memory for a differnt object to store it.



To do what you are trying to do, you need to take the name of the color and translate it into a color. One way to do this is with a series of if

/ operators else

that check the contents of the string and assign the appropriate color if it matches the color name.
Fortunately, there is an easier way. You can just parse the string into ConsoleColor

!

Following an example from MSDN, you can do this:

Console.BackgroundColor = (ConsoleColor) Enum.Parse(typeof(ConsoleColor), s);

      

+3


source


Console.BackgroundColor = (ConsoleColor)Enum.Parse(typeof (ConsoleColor), s);

      



+2


source


ConsoleColor is an enumeration. You need to click s

.

public static void SetBG(string s){
   Console.BackgroundColor = (ConsoleColor) Enum.Parse(typeof(ConsoleColor), s);
}

      

+2


source


This is because ConsoleColor is an enum, try

 Console.BackgroundColor = (ConsoleColor) Enum.Parse(typeof(ConsoleColor), s);

      

You should consider using if the string is not valid (eg s = "Ship") with something like this

 ConsoleColor colorValue;
 if (Enum.TryParse(s, out colorValue))
 {        
     Console.BackgroundColor = colorValue;
 }

      

+2


source


ConsoleColor colour;
if (ConsoleColor.TryParse(s, out colour))
{
   Console.BackgroundColor = colour;
}

      

+2


source


To add to all the rest of the use of the method Enum.Parse

, as of .NET 4.0 you can use a Enum.TryParse

generic method
to better handle invalid values:

public static void SetBG(string s)
{
    ConsoleColor color;

    if (!Enum.TryParse<ConsoleColor>(s, out color))
        throw new ArgumentException("s");

    Console.BackgroundColor = color;
}

      

+1


source







All Articles