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.
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.
Short answer: stop using an exception for an expected UI validation case and validate earlier. 's Console.Write didn't show because this is a GUI app (Console.Write writes to a console window only). 's advice to "show a message" is correct and is right that the logic is confusing because "English" is being tested in several places — simplify the flow and give the user a clear, actionable message instead of throwing.
Validate inputs first, then do the conversion. Example pattern (validate length, width and selection; show a MessageBox and focus the offending control):
using System.Globalization;
decimal length, width;
if (!decimal.TryParse(textBoxLength.Text.Trim(), NumberStyles.Number, CultureInfo.CurrentCulture, out length))
{
MessageBox.Show("Please enter a valid numeric length.", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
textBoxLength.Focus();
return;
}
if (!decimal.TryParse(textBoxWidth.Text.Trim(), NumberStyles.Number, CultureInfo.CurrentCulture, out width))
{
MessageBox.Show("Please enter a valid numeric width.", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
textBoxWidth.Focus();
return;
}
if (comboBoxDieUnits.SelectedIndex < 0)
{
MessageBox.Show("Select die units (English or Metric).", "Selection required", MessageBoxButtons.OK, MessageBoxIcon.Information);
comboBoxDieUnits.Focus();
return;
} Replace the default throw with user feedback (MessageBox or an ErrorProvider) so the app remains usable:
default:
MessageBox.Show("Unknown unit selection. Choose English or Metric from the list.", "Invalid selection", MessageBoxButtons.OK, MessageBoxIcon.Warning);
comboBoxDieUnits.Focus();
break; Quick best-practice notes: set the ComboBox to DropDownList to prevent free-text entries, Trim() and compare with StringComparison.OrdinalIgnoreCase, factor conversion logic into a helper method that converts via a canonical unit, and use named constants for conversion/offset values. Reserve exceptions for truly unexpected situations and use an ErrorProvider or inline label for gentler UX. 's point about handling exceptions at a higher level still applies for real errors; user input is better handled with validation.
Jump to Post— Rashakil Fol 978By doing just what you described.
What knowledge are you lacking that is preventing you from implementing this behavior? (Bringing up a message? Something else?)
Jump to Post— Ramy Mahrous 401You 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.
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?)
I tried a console.write ("instructions"); break;
but nothing comes up when I test it.
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.
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");
}
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"
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...
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.