0

When i write case statements my last line is always:

default:
throw new Exception("Unknown selection");

how can I change this so a message comes up for the user to do something with the data he or she inputted so the default will not occur.

5
Contributors
6
Replies
7
Views
8 Years
Discussion Span
Last Post by sknake
0

By doing just what you described.

What knowledge are you lacking that is preventing you from implementing this behavior? (Bringing up a message? Something else?)

0

I tried a console.write ("instructions"); break;
but nothing comes up when I test it.

0

You can't give dynamic statement as it's your logic exception, the method calls this method (which contains this switch...case) will handle this throw with meaningful error message to the user.
I really don't understand your question, give a scenario.

0

Here is my code. Can you help see what to change. Im sorry but Im lost. Basically I want a message to come up if this code fails because a proper variable is not imputted.

        decimal blankl;
        decimal blankw;            
        decimal.TryParse(blanklTB.Text,out blankl);
        decimal.TryParse(blankwTB.Text, out blankw);

        if (bunitsCB.Text == "English")
            {                
                switch (dieunitsCB.Text)
                {
                    case "English":
                        dielTB.Text = Convert.ToString(blankl + Convert.ToDecimal(24));
                        diewTB.Text = Convert.ToString(blankw + Convert.ToDecimal(24)); break;
                    case "Metric":
                        dielTB.Text = Convert.ToString(Math.Round((blankl * Convert.ToDecimal(25.4)), 0) + Convert.ToDecimal(588));
                        diewTB.Text = Convert.ToString(Math.Round((blankw * Convert.ToDecimal(25.4)), 0) + Convert.ToDecimal(588)); break;
                    default:
                        throw new Exception("Unknown selection");
                }
             }
            else
            {
                switch (dieunitsCB.Text)
                {
                    case "English":
                        dielTB.Text = Convert.ToString(Math.Round((blankl / Convert.ToDecimal(25.4)), 0) + Convert.ToDecimal(24));
                        diewTB.Text = Convert.ToString(Math.Round((blankw / Convert.ToDecimal(25.4)), 0) + Convert.ToDecimal(24)); break;                           
                    case "Metric":                            
                        dielTB.Text = Convert.ToString(blankl + Convert.ToDecimal(588));
                        diewTB.Text = Convert.ToString(blankw + Convert.ToDecimal(588)); break;
                    default:
                        throw new Exception("Unknown selection");
                }

Edited by mike_2000_17: Fixed formatting

0

Please use code tags - that code is hard to read correctly without - and its so simple to put them in.

OK, your code is unclear if nothing else because you have a test to see if dunitsCB.Text says english or not..

You then switch on dieunitsCB.Text or dieunitsCB.Text (eg the same thing) to produce different answers. Its confusing as you have 3 different tests for "English" making it confusing.

So, your problem seems to be you want to complain if neither English or Metric arent selected - why go through all that hassle, why not just test that this is not the case and "do something"

0

Seeing how this is an extension of the thread you started: http://www.daniweb.com/forums/thread176030.html

Where I provided you with the exception on the default of "unknown selection" but you didn't update the original thread or mark it solved i'm not seeing a good reason to continue to help...

Rashakil Fol gave you the answer you're looking for, though...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.