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);
Kahbazi
  • 14,331
  • 3
  • 45
  • 76
petrtim
  • 267
  • 1
  • 2
  • 10
  • 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 – Sehnsucht Jul 08 '16 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. – Richard Barker Jul 08 '16 at 20:13
  • @Sehnsucht That's why he want's an `Enum of strings`, like java lets you do – Sam I am says Reinstate Monica Jul 08 '16 at 20:13
  • @SamIam I understood ; I just explained why it's not possible – Sehnsucht Jul 08 '16 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. – Servy Jul 08 '16 at 20:16
  • yes methods cannot be applied to enums. to however i feel that's bad practice. But that's probably just me. – Richard Barker Jul 08 '16 at 20:21

1 Answers1

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 ");
}
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Servy
  • 202,030
  • 26
  • 332
  • 449