Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# equivalent of the Ruby symbol

Tags:

c#

symbols

ruby

I'm developing a little C# application for the fun. I love this language but something disturb me ...

Is there any way to do a #define (C mode) or a symbol (ruby mode).

The ruby symbol is quite useful. It's just some name preceded by a ":" (example ":guy") every symbol is unique and can be use any where in the code.

In my case I'd like to send a flag (connect or disconnect) to a function.

What is the most elegant C# way to do that ?

Here is what i'd like to do :

BgWorker.RunWorkersAsync(:connect)
//...

private void BgWorker_DoWork(object sender, DoWorkEventArgs e)
{
  if (e.Arguement == :connect)
    //Do the job
}

At this point the my favorite answer is the enum solution ;)

like image 807
Nicolas Guillaume Avatar asked Feb 03 '23 05:02

Nicolas Guillaume


1 Answers

In your case, sending a flag can be done by using an enum...

public enum Message
{
  Connect,
  Disconnect
}

public void Action(Message msg)
{
   switch(msg)
   {
      case Message.Connect: 
         //do connect here
       break;
      case Message.Disconnect: 
          //disconnect
       break;
      default:
          //Fail!
       break;
   }
}
like image 65
jmservera Avatar answered Feb 07 '23 10:02

jmservera