Reputation: 93
Hi there I'am quite new to c# and WPF and was wondering if anyone could help me with a problem that I am currently having. I am trying to overload 3 methods in a very simple and basic wpf app (just to see how/if it works) but at run time when I try to check option one or two the application reports and error and closes. However If I check option 3 the application runs as intended. Any one that has any hints tips or solutions would be great. (Here is a code snippet of my basic app).
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void calculate_Click(object sender, RoutedEventArgs e)
{
int val = int.Parse(first.Text);
int val1 = int.Parse(second.Text);
int val2 = int.Parse(third.Text);
if ((bool)oneValue.IsChecked)
showTotal(val);
else if ((bool)twoValues.IsChecked)
showTotal(val, val1);
else if ((bool)threeValues.IsChecked)
showTotal(val, val1, val2);
}
private void showTotal(int val, int val1, int val2)
{
val = int.Parse(first.Text);
val1 = int.Parse(second.Text);
val2 = int.Parse(third.Text);
int total = val + val1 + val2;
result.Text = total.ToString();
}
private void showTotal(int val, int val1)
{
val = int.Parse(first.Text);
val1 = int.Parse(second.Text);
int total = val + val1;
result.Text = total.ToString();
}
private void showTotal(int val)
{
val = int.Parse(first.Text);
result.Text = val.ToString();
}
private void quit_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
Upvotes: 0
Views: 1068
Reputation: 101701
1) You are trying to parse your values twice
2) here Your bool cast is redundant because IsChecked already bool
if ((bool)oneValue.IsChecked)
3) Instead of parse, use TryParse it won't throw exception
int val, val1, val2;
if (oneValue.IsChecked)
{
if (int.TryParse(first.Text, val))
{
showTotal(val);
}
}
else if (twoValues.IsChecked)
{
if (int.TryParse(second.Text, val1))
{
showTotal(val, val1);
}
}
else if (threeValues.IsChecked)
{
if (int.TryParse(third.Text, val2)
{
showTotal(val, val1, val2);
}
}
4) Also you can use params keyword instead of method overloading.I guess it is more suitable in your case.With params keyword you can do your job with one method like this:
private void showTotal(params int[] numbers)
{
if(numbers != null)
{
int sum = numbers.Sum();
result.Text = sum.ToString();
}
}
Upvotes: 1
Reputation: 2682
I see two things wrong with this code. The first is that I'm going to guess that you're having input validation in that the text can't be parsed into an integer. As a best practice, use int.TryParse, i.e.
int val;
if (!int.TryParse(first.Text, out val))
{
// Handle error
}
The second issue is that you're basically overwriting any parameters you're passing in. Why not just either have different methods for the # of inputs, or take a single parameter that indicates how many inputs you should use.
In fact, I'm not sure that overloading here is the correct approach. Why not use params:
private void showTotal(params string[] inputs)
{
int total = 0;
foreach (string input in inputs)
{
int val;
if (int.TryParse(input, out val))
total += total;
}
result.Text = total.ToString();
}
You can call it like
if ((bool)oneValue.IsChecked)
showTotal(first.Text);
else if ((bool)twoValues.IsChecked)
showTotal(first.Text, second.Text);
else if ((bool)threeValues.IsChecked)
showTotal(first.Text, second.Text, third.Text);
Upvotes: 1
Reputation: 3579
Your overloads look correct to me. You probably don't have anything in your second or thrid text box if the second or third checkbox is not checked (it's hard to tell without more details). So the first thing you are doing is parsing all three text boxes, but if it's empty then you will get an exception. You can use TryParse which attempts to parse the string, but if it can not just returns false and you can gracefully handle it.
What I would do is something like this:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void calculate_Click(object sender, RoutedEventArgs e)
{
int val, val1, val2;
if (!int.TryParse(first.Text, out val))
{
val=0; // Invalid or blank input get's a zero (or you could show an error message)
}
if (!int.TryParse(second.Text, out val1))
{
val1=0; // Invalid or blank input get's a zero (or you could show an error message)
}
if (!int.TryParse(third.Text, out val2))
{
val2=0; // Invalid or blank input get's a zero (or you could show an error message)
}
if ((bool)oneValue.IsChecked)
showTotal(val);
else if ((bool)twoValues.IsChecked)
showTotal(val, val1);
else if ((bool)threeValues.IsChecked)
showTotal(val, val1, val2);
}
private void showTotal(int val, int val1, int val2)
{
int total = val + val1 + val2;
result.Text = total.ToString();
}
private void showTotal(int val, int val1)
{
int total = val + val1;
result.Text = total.ToString();
}
private void showTotal(int val)
{
result.Text = val.ToString();
}
private void quit_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
Upvotes: 1
Reputation: 4296
Are you sure that the value of first.Text
, second.Text
, and third.Text
are integer values? Calling int.Parse()
on a non-integer value will throw an error. You might want to look into using int.TryParse()
instead:
private void showTotal(int val)
{
bool gotResult = int.TryParse(first.Text, out val);
if(gotResult)
result.Text = val.ToString();
}
Furthermore, I'm pretty sure you application is failing on the "one" and "two" scenarios because this code:
private void calculate_Click(object sender, RoutedEventArgs e)
{
int val = int.Parse(first.Text);
int val1 = int.Parse(second.Text);
int val2 = int.Parse(third.Text);
Will fail unless first
, second
, and third
all contain values. If second
or third
contains null or an empty string, you will get an error from int.Parse
.
Upvotes: 0