C# 4.0: Can I use a Color as an optional parameter with a default value?

Posted by DTown on Stack Overflow See other posts from Stack Overflow or by DTown
Published on 2010-05-10T16:27:07Z Indexed on 2010/05/10 16:34 UTC
Read the original article Hit count: 248

Filed under:
|
    public void log(String msg, Color c = Color.black)
    {
        loggerText.ForeColor = c;
        loggerText.AppendText("\n" + msg);

    }

This results in an error that c must be a compile-time constant. I've read up on this a little and most examples are dealing with strings and ints. I've figured out I can use the colorconverter class but I'm not sure it will be very efficient. Is there a way to just pass a basic color as an optional parameter?

    public void log(String msg, String c = "Black")
    {
        ColorConverter conv = new ColorConverter();
        Color color = (Color)conv.ConvertFromString(c);

        loggerText.ForeColor = color;
        loggerText.AppendText("\n" + msg);
    }

© Stack Overflow or respective owner

Related posts about c#

Related posts about optional-parameters