I have a Java application and I'm porting it to C #. I have a question regarding enum
that seems to work differently than Java. After you have asked a question here on the site ( Enumerations can contain abstract methods? ), whenever possible opt to centralize methods, and properties in enum
. Well, in Java my code looks like this:
enum BBcodes {
BOLD("b"),
ITALIC("i"),
//...
UNDERLINE("u");
private final String code;
public BBcodes(String code){
this.code = code;
}
public string wrap(String contents){
return MessageFormat.format("[{0}]{1}[/{0}]", this.code, contents);
}
}
In which I can call it like this:
String bbcode = BBcodes.BOLD.wrap("StackOverflow"); //[b]StackOverflow[/b]
I saw on this question that I can not create a enum
that stores values of type string
. I do not know if this is the best way to solve it, but I created a class for it:
public class BBcodes
{
public static readonly string BOLD = "b";
public static readonly string ITALIC = "i";
//...
public static string Wrap(string code, string contents)
{
return string.Format("[{0}]{1}[/{0}]", code, contents);
}
}
In what I call it:
string bbcode = BBcodes.Wrap(BBcodes.BOLD, "StackOverflow"); //[b]StackOverflow[/b]
It is not a problem to do this, having to pass its BBCode value as argument to the Wrap
method. It's something to get used to. :)
But if possible, I would like to do something as close to Java as possible, creating everything (the values and methods) in enum
itself. How could I do this in C #? It's possible?