8
I have an application in Java and am porting it to C#. I have a question regarding enum
that seems to work differently from Java. After asking a question here on the site (Enumerations may contain abstract methods?), whenever possible I choose to centralize methods and properties in a enum
. Well, in Java my code is 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 him that:
String bbcode = BBcodes.BOLD.wrap("StackOverflow"); //[b]StackOverflow[/b]
Vi in that question I can’t create a enum
which holds values of the kind string
. I don’t 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 which I call it:
string bbcode = BBcodes.Wrap(BBcodes.BOLD, "StackOverflow"); //[b]StackOverflow[/b]
It is not a problem to do so, having to pass the respective value of Bbcode as argument to the method Wrap
. It’s something you get used to. :)
But if possible, I’d like to do something as close as possible to Java, creating everything (the values and methods) in itself enum
. How could I do this in C#? Is it possible?
I love this kind of question, it will certainly be useful for some user in the future.
– Zignd