locked
'sender' parameter not working with switch/case block? RRS feed

  • Вопрос

  • Hello,
        For some reason unkown to me, whenever I try and use a switch/case block with the sender parameter, I get the error: "A value of an integral type expected", yet if I try the exact same thing using if and else if blocks, it works fine. Why?
        For example: I set two button's click event to call the same event. I then want to be able to tell who the sender was, so that the form will act accordingly, but can only seem to get this to work with if blocks, not switch/case blocks.


    Thanks!
    chukrum47
    How are a plum and a rabbit similar? They're both purple, except for the rabbit.
    8 октября 2008 г. 22:47

Ответы

  • "switch" will only work with "integral types" and strings.  Integral types are int, short, long, byte, char, Uint16, etc.  Also watch out for the "case" statement that requires a constant value


    Les Potter, Xalnix Corporation, Yet Another C# Blog
    • Помечено в качестве ответа chukrum47 9 октября 2008 г. 1:01
    9 октября 2008 г. 0:34
  • chukrum47 said:

    Hello JohnWein,
        Sorry but I'm not sure what I cast sender to? What does that mean?


    Hello Rudedog,
        Here is an example of code. The switch block errors out, while if blocks work fine. The two senders are button1 and button2.

    1 private void ButtonClick(object sender, EventArgs e)  
    2         {  
    3             switch (sender)  
    4             {  
    5                 case this.button1:  
    6                     this.BackColor = Color.AntiqueWhite;  
    7                     break;  
    8                 case this.button2:  
    9                     this.BackColor = Color.Honeydew;  
    10                     break;  
    11             }  
    12             if (sender == button1)  
    13                 this.BackColor = Color.Khaki;  
    14             else if (sender == button2)  
    15                 this.BackColor = Color.LawnGreen;  
    16         } 



    Thanks!
    chukrum47
    How are a plum and a rabbit similar? They're both purple, except for the rabbit.



    You are switching type.  The expression does not evaluate to a value type.  That is what the error message is saying.

    Rudedog   =8^D
    Mark the best replies as answers. "Fooling computers since 1971."
    • Помечено в качестве ответа chukrum47 9 октября 2008 г. 1:01
    9 октября 2008 г. 0:47
  • For your example, use "if" and "else".  You could use the button1.Tag and code your own integer value, but that would begin to obscure the code for no obvious performance gain.

        button1.Tag = 1;
        button2.Tag = 2;

        private void ButtonClick(object sender, EventArgs e)
        {
            Button ctrl = sender as Button;
            switch((int)ctrl.Tag)
            {
                case 1:
                    ....
                case 2:
                    ....
            }
        }

    Again, I don't recommend this because now if you add a Button3, you have 2 places that need to be modified, not just one.  Not to mention, this wastes a user data holder (Tag) for a trivial use that can be solved less obscurely with if-then-else.
    Les Potter, Xalnix Corporation, Yet Another C# Blog
    • Помечено в качестве ответа chukrum47 9 октября 2008 г. 21:58
    9 октября 2008 г. 11:33
  • You can use strings in a switch box:
     

        void BtnsClick(object sender, EventArgs e)

        {

          switch (((Button)sender).Text)

          {

            case "button1":

              //...

              break;

            case "button2":

              //...

              break;

          }

        }

    • Помечено в качестве ответа chukrum47 9 октября 2008 г. 21:58
    9 октября 2008 г. 11:58

Все ответы

  • What do you cast sender to?
    8 октября 2008 г. 22:55
  • chukrum47 said:

    Hello,
        For some reason unkown to me, whenever I try and use a switch/case block with the sender parameter, I get the error: "A value of an integral type expected", yet if I try the exact same thing using if and else if blocks, it works fine. Why?
        For example: I set two button's click event to call the same event. I then want to be able to tell who the sender was, so that the form will act accordingly, but can only seem to get this to work with if blocks, not switch/case blocks.


    Thanks!
    chukrum47


    How are a plum and a rabbit similar? They're both purple, except for the rabbit.



    What comparison are you making in the switch?  The comparison must evaluate to a boolean result.  You cannot compare against System.Type, for whatever reason.  Besides, comparing against type is not the direction you want to go in.  The type of sender should be pretty much nailed down and known when the event occurs.

    Is this a custom event?  Create a class derived from EventArgs that carries enough information to discern the sender's type.  For Example, the Click event on a button returns different types for EventArgs depending upon if the user clicks with the mouse or presses Enter with the keyboard.

    Rudedog  =8^D
    Mark the best replies as answers. "Fooling computers since 1971."
    • Изменено Rudedog2 8 октября 2008 г. 23:08 EventArgs
    8 октября 2008 г. 23:00
  • Hello JohnWein,
        Sorry but I'm not sure what I cast sender to? What does that mean?


    Hello Rudedog,
        Here is an example of code. The switch block errors out, while if blocks work fine. The two senders are button1 and button2.
    1 private void ButtonClick(object sender, EventArgs e)  
    2         {  
    3             switch (sender)  
    4             {  
    5                 case this.button1:  
    6                     this.BackColor = Color.AntiqueWhite;  
    7                     break;  
    8                 case this.button2:  
    9                     this.BackColor = Color.Honeydew;  
    10                     break;  
    11             }  
    12             if (sender == button1)  
    13                 this.BackColor = Color.Khaki;  
    14             else if (sender == button2)  
    15                 this.BackColor = Color.LawnGreen;  
    16         } 



    Thanks!
    chukrum47
    How are a plum and a rabbit similar? They're both purple, except for the rabbit.
    8 октября 2008 г. 23:14
  • "switch" will only work with "integral types" and strings.  Integral types are int, short, long, byte, char, Uint16, etc.  Also watch out for the "case" statement that requires a constant value


    Les Potter, Xalnix Corporation, Yet Another C# Blog
    • Помечено в качестве ответа chukrum47 9 октября 2008 г. 1:01
    9 октября 2008 г. 0:34
  • chukrum47 said:

    Hello JohnWein,
        Sorry but I'm not sure what I cast sender to? What does that mean?


    Hello Rudedog,
        Here is an example of code. The switch block errors out, while if blocks work fine. The two senders are button1 and button2.

    1 private void ButtonClick(object sender, EventArgs e)  
    2         {  
    3             switch (sender)  
    4             {  
    5                 case this.button1:  
    6                     this.BackColor = Color.AntiqueWhite;  
    7                     break;  
    8                 case this.button2:  
    9                     this.BackColor = Color.Honeydew;  
    10                     break;  
    11             }  
    12             if (sender == button1)  
    13                 this.BackColor = Color.Khaki;  
    14             else if (sender == button2)  
    15                 this.BackColor = Color.LawnGreen;  
    16         } 



    Thanks!
    chukrum47
    How are a plum and a rabbit similar? They're both purple, except for the rabbit.



    You are switching type.  The expression does not evaluate to a value type.  That is what the error message is saying.

    Rudedog   =8^D
    Mark the best replies as answers. "Fooling computers since 1971."
    • Помечено в качестве ответа chukrum47 9 октября 2008 г. 1:01
    9 октября 2008 г. 0:47
  • Hello xalnix and Rudedog2,
        Thanks explaining! Is there any way around this, or do I have to use only if and else if statements?


    Thanks!
    chukrum47
    How are a plum and a rabbit similar? They're both purple, except for the rabbit.
    9 октября 2008 г. 1:01
  • For your example, use "if" and "else".  You could use the button1.Tag and code your own integer value, but that would begin to obscure the code for no obvious performance gain.

        button1.Tag = 1;
        button2.Tag = 2;

        private void ButtonClick(object sender, EventArgs e)
        {
            Button ctrl = sender as Button;
            switch((int)ctrl.Tag)
            {
                case 1:
                    ....
                case 2:
                    ....
            }
        }

    Again, I don't recommend this because now if you add a Button3, you have 2 places that need to be modified, not just one.  Not to mention, this wastes a user data holder (Tag) for a trivial use that can be solved less obscurely with if-then-else.
    Les Potter, Xalnix Corporation, Yet Another C# Blog
    • Помечено в качестве ответа chukrum47 9 октября 2008 г. 21:58
    9 октября 2008 г. 11:33
  • You can use strings in a switch box:
     

        void BtnsClick(object sender, EventArgs e)

        {

          switch (((Button)sender).Text)

          {

            case "button1":

              //...

              break;

            case "button2":

              //...

              break;

          }

        }

    • Помечено в качестве ответа chukrum47 9 октября 2008 г. 21:58
    9 октября 2008 г. 11:58
  • JohnWein said:

    You can use strings in a switch box:
     

        void BtnsClick(object sender, EventArgs e)

        {

          switch (((Button)sender).Text)

          {

            case "button1":

              //...

              break;

            case "button2":

              //...

              break;

          }

        }



    I like John's snippet.  I come from the school of thought that says anything you can do with "if" can be re-written using "switch".  I don't know about you, but I find switch to be much more readable.

    Integral Types in .NET

    Make sure your switch statement evaluates to one of the above types, or a string.  The IDE and the snippet editor like it when you use an enum in your switch condition, too.

    Rudedog   =8^D

    Mark the best replies as answers. "Fooling computers since 1971."
    9 октября 2008 г. 12:23
  • Hello JohnWein,
        Thanks! It worked perfectly!

    Hello Rudedog2,
        I have to agree with you: if something can be done with "if" it should be able to be done with "switch", as "switch" blocks are much more readable.

    Thanks again all who helped me!
    chukrum47


    How are a plum and a rabbit similar? They're both purple, except for the rabbit.
    9 октября 2008 г. 22:00
  • Thank you. friend helped me a lot.
    10 сентября 2019 г. 16:59