0

have this class:

public static class Command
{
    public const string SET_STB_MEDIA_CTRL = "SET STB MEDIA CTRL ";
    public static string ECHO = "ECHO";
    public static string SET_CHANNEL = "SET CHANNEL ";
    public static string GET_VOLUMN = "GET VOLUMN";
    public static string GET_MAX_VOLUMN = "GET MAX VOLUMN ";
    public string SET_STB_MEDIA_LIST = "SET STB MEDIA LIST ";
}

then:

public static class MultimediaConstants
{
    public const string VIDEO = "video";
    public const string AUDIO = "audio";
    public const string PHOTO = "photo";
    public const string ALL = "all";
    public const string BACKGROUND_MUSIC = "background_music";
    public const string TV = "tv";
    public const string ACTION_PLAY = "play";
}

point is, that I would like to have something like this:

public static string SET_STB_MEDIA_CTRL (MultimediaConstants type,  MultimediaConstants action)
{
    return Command.SET_STB_MEDIA_CTRL + "type:" + type + "action:" + action;
}

So the result of this method should be:

 SET STB MEDIA CTRL type:tv action:play

The call of the method will be:

 SET_STB_MEDIA_CTRL (MultimediaConstants.TV, MultimediaConstants.ACTION_PLAY);
6
  • 2
    you can't ask for an instance of a static class as method argument because you can't create instance of a static class Commented Jul 8, 2016 at 20:12
  • 1
    These arent enums. these are classes. you can create an enum using the enum keyword instead of class. then you can use the values like you want. Commented Jul 8, 2016 at 20:13
  • @Sehnsucht That's why he want's an Enum of strings, like java lets you do Commented Jul 8, 2016 at 20:13
  • @SamIam I understood ; I just explained why it's not possible Commented Jul 8, 2016 at 20:14
  • @RichardBarker They're conceptually enums. It's an appropriate use of the term in a general CS sense. C#'s implementation of enums isn't capable of accomplishing this, however. That makes these not C# enums, but calling them enums in a general sense is still applicable. Commented Jul 8, 2016 at 20:16

1 Answer 1

3

The way to approach problems like this is for the class in question to have a private constructor, and to have public static field/properties that are initialized with values of that instance. This is a way of having a fixed finite number of immutable instances of that type, while still allowing methods to accept parameters of that type.

The following code is valid C# 6.0.

public class Command
{
    private Command(string value)
    {
        Value = value;
    }

    public string Value { get; private set; }

    public static Command SET_STB_MEDIA_CTRL { get; } = new Command("SET STB MEDIA CTRL ");
    public static Command ECHO { get; } = new Command("ECHO");
    public static Command SET_CHANNEL { get; } = new Command("SET CHANNEL ");
    public static Command GET_VOLUMN { get; } = new Command("GET VOLUMN");
    public static Command GET_MAX_VOLUMN { get; } = new Command("GET MAX VOLUMN ");
    public static Command SET_STB_MEDIA_LIST { get; } = new Command("SET STB MEDIA LIST ");
}
Sign up to request clarification or add additional context in comments.

4 Comments

don't forget to make them readonly!
@DanielA.White, since they're getter-only properties, they're automagically readonly
ah didn't notice that this was c# 6
@Servy thanks, works perfect!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.