Hack4Life
Hack4Life

Reputation: 563

Compiler Error CS0120 in C#

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?

Upvotes: 1

Views: 964

Answers (1)

Jenish Rabadiya
Jenish Rabadiya

Reputation: 6766

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.

Upvotes: 3

Related Questions