Friday, August 13, 2010

Extension methods to extend class members?

Programmer Question

Hi all. I'd like to write an extension method that extends some member of my class. Specifically, I'd like to extend an enum. However, this is not working:



namespace mynamespace
{
public class myclass
{
public enum ErrorCodes
{
Error1, Error2, Error3
}

public static double GetDouble(this ErrorCodes ErrorCode)
{
return (double)((int)ErrorCode);
}

public void myfunc()
{
ErrorCodes mycode;
MessageBox.Show(mycode.GetDouble().ToString());
}
}
}


Specifically, it doesn't recognize GetDouble() as an extension. This is not just for enums either, I tried creating an extension method for doubles and had the same problem, too.



Find the answer here

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails