0

I'm wondering how in C# I would go about creating a method that would take a couple set values, kind of like an enum.

public static void SwitchState(enum State { On, Off, Idle }) {

}

Something like that I don't know...I don't want to have strings passed into the method because then I have to memorize what those strings are.

Mark Aven
  • 325
  • 3
  • 8

4 Answers4

0
public enum State { On, Off, Idle }    

public static void SwitchState(State state) {
   // ...
}
Ivan Vargas
  • 703
  • 3
  • 9
0

You actually can use enums like this

public enum SpotifyAction
{
            PlayPause,
            Next,
            Previous,
            VolumeUp,
            VolumeDown,
            Mute,
            Quit
};

public static void controlSpotify(SpotifyAction sa)
{
            BringToForeground("Spotify");

            switch (sa)
            {
                case SpotifyAction.Next:
                    SendKeys.SendWait("^({RIGHT})");
                    break;
                case SpotifyAction.Previous:
                    SendKeys.SendWait("^({LEFT})");
                    break;
                case SpotifyAction.VolumeUp:
                    SendKeys.SendWait("^({UP})");
                    break;
                case SpotifyAction.VolumeDown:
                    SendKeys.SendWait("^({DOWN})");
                    break;
                case SpotifyAction.PlayPause:
                    SendKeys.SendWait(" ");
                    break;
            }
}
online Thomas
  • 8,864
  • 6
  • 44
  • 85
0

You just create an enum and then pass it to the method.

public enum State
{
        On, 
        Off,
        Idle
}

private void SwitchState(State stateValue)
{

}

private void testCall()
{
    SwitchState(State.Idle);
}
0
void Main()
{
    SwitchState(State.On | State.Off);
}

public void SwitchState(State s)
{
    //...
}

public enum State
{
    None = 0,
    On = 1,
    Off = 2,
    Idle = 4
}
Greg
  • 1,076
  • 1
  • 9
  • 17