none
Macros equivalent to pass the conditions into switch in c# RRS feed

  • Question

  • I have a c# application  . I want to use a switch condition to check the status of the system . I want to pass IDLE, COUNT, Collect states in a switch condition . i cannot use macros to define the states . Please suggest an alternative way
    Wednesday, January 8, 2020 12:20 PM

Answers

  • Reading between the lines, I suspect you are looking for an "enum".

    enum State {
        Idle,
        Count,
        Collect
    };
    ...
    State systemState = State.Idle;
    ...
    switch( systemState )
    {
        case State.Idle:
            ...
            break;
        case State.Count:
            ...
            break;
    }



    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    • Marked as answer by varun_bwazz Thursday, January 9, 2020 8:41 AM
    Thursday, January 9, 2020 1:26 AM

All replies

  • In regards to switch condition, were is the data e.g. IDLE, COUNT coming from and were in the project is this going to take place? More specific details are better than less details when replying.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, January 8, 2020 2:29 PM
    Moderator
  • Post the code you're trying to replicate because even in C++ you couldn't pass 3 values to a single switch condition. C++ required integral expressions. It is unclear how you would get a "macro" to do something like this.

    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, January 8, 2020 9:16 PM
    Moderator
  • Reading between the lines, I suspect you are looking for an "enum".

    enum State {
        Idle,
        Count,
        Collect
    };
    ...
    State systemState = State.Idle;
    ...
    switch( systemState )
    {
        case State.Idle:
            ...
            break;
        case State.Count:
            ...
            break;
    }



    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    • Marked as answer by varun_bwazz Thursday, January 9, 2020 8:41 AM
    Thursday, January 9, 2020 1:26 AM
  • Hi,
    Based on your description, Could you explain in detail what the status of the system is? And what is relationship between IDLE, COUNT, Collect states and status of the system?
    If possible, please provide the relevant code.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, January 9, 2020 8:35 AM
  • I have attached the code. Basically this is a event handler for serial communication. Data is received from the serial port

    The problem is to change the state when the data is received . initially the state should be idle . When the data is received , the state will turn to active and then go back to idle . so the data received should be taken in an array and use it for different states. Please help

    // Code

    private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {

                DataIn = (UInt16)serialPort1.ReadByte();
                this.Invoke(new EventHandler(Showdata));

            }

               
            private void Showdata(object sender, EventArgs e)
            {
                     
                  if (DataIn == 17)
                  {
                      textBox2.Text +=   DataIn.ToString();
                      pictureBox1.BackColor = Color.Green;

                  }
                  else if (DataIn == 18)
                  {

                      textBox2.Text +=   DataIn.ToString();

                      pictureBox1.BackColor = Color.Red;

                  }
                else if(DataIn == 129)
                  {
                      c[0] = (int)161;
                      textBox2.Text +=  DataIn.ToString();
                      serialPort1.Write(c, 0, 1);

                  }
                  else
                  {
                      textBox2.Text += DataIn.ToString();
                  } 
                  
            }
    Thursday, January 9, 2020 9:48 AM
  • I don't see any macros here but a simple switch statement works just fine. However since you're transferring data I'd recommend adding a parameter to your method.

    You could use an enum as Tim mentioned if your # of commands is fixed and small otherwise stick with the int and use named constants instead.

    private void serialPort1_DataReceived (…)
    {
       var command = serialPort1.ReadByte();
       ShowData(command);   
    }
    
    private delegate void HandleCommand ( byte command );
    
    private void ShowData ( byte command )
    {
       if (InvokeRequired)
       {
          Invoke((HandleCommand)ShowData, new object[] { command });
          return;
       };
    
       //All commands seem to do this...
       textBox2.Text += command.ToString();
    
       switch(command)
       {
          case 17: 
          {
             pictureBox1.BackColor = Color.Green;
             break;
          };
          case 18:
          {
             pictureBox1.BackColor = Color.Red;
             break;
          };
          case 129:
          {        
             c[0] = 161;
             serialPort1.Write(c, 0, 1);
             break;
          };
       };
    }


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, January 9, 2020 2:51 PM
    Moderator