1

I try to convert a string which contains a hex code to a color. I have the following code:

CQ currCQ = new CQ();     
string color_startBorderMC = null;
color_startBorderMC = currCQ._color_MCBorder; //returns string! e.g. #ff00ff
Color _startBorderMC_color = new Color();
_startBorderMC_color = ColorConverter.ConvertFromString(color_startBorderMC); //error

If I write an method for getting the Color String I still get the same error:

An object reference is required for the non-static field, method, or property 'System.ComponentModel.TypeConverter.ConvertFromString(string)'

The method for getting the color string is this:

internal string getMCBorderColor()
{
    return this._color_MCBorder;
}

My Object CQ has the following definition:

public class CQ
{
    public string   _color_mostcriticallBorder  {set; get; };
}

How can I fix this error?

4
  • Still the same error An object reference is required for the non-static field, method, or property 'System.ComponentModel.TypeConverter.ConvertFromString(string)' @Nameismy Commented Aug 24, 2015 at 11:33
  • Sidenote: Naming Guidelines Commented Aug 24, 2015 at 11:33
  • Are you referring to System.Drawing.Color or System.Windows.Media.Color? Commented Aug 24, 2015 at 11:34
  • System.Drawing.Color - I'm using it in an ASP.NET project. I've forgot to mention it above sorry. Commented Aug 24, 2015 at 11:35

1 Answer 1

3

You need to create an instance of ColorConverter class in order to access the method ConvertFromString

CQ currCQ = new CQ();     
string color_startBorderMC = null;
color_startBorderMC = currCQ._color_MCBorder; //returns string! e.g. #ff00ff
Color _startBorderMC_color = new Color();
ColorConverter converter = new ColorConverter();//create an instance of ColorConverter.
_startBorderMC_color = converter.ConvertFromString(color_startBorderMC);

for more information refer this msdn documentation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.