Try- catch clauses with C# calculator help

I need to add try catch clauses

Page 1 of 1

7 Replies - 4421 Views - Last Post: 09 November 2009 - 10:43 AM Rate Topic: -----

#1 jmartin51  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 25-September 09

Try- catch clauses with C# calculator help

Posted 09 November 2009 - 09:41 AM

ok so I have this very basic calculator program in C# windows application
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace _
{
	public partial class calculator : Form
	{
		bool plus = false;
		bool equal = false;
		bool minus = false;
		public calculator()
		{
			InitializeComponent();
		}
		private void bt1_Click(object sender, EventArgs e)
		{
			this.output.Text += "1";
		}
		
		private void bt2_Click(object sender, EventArgs e)
		{
			this.output.Text += "2";
		}

		private void bt7_Click(object sender, EventArgs e)
		{
			this.output.Text += "7";
		}

		private void bt3_Click(object sender, EventArgs e)
		{
			this.output.Text += "3";
		}

		private void bt4_Click(object sender, EventArgs e)
		{
			this.output.Text += "4";
		}

		private void bt5_Click(object sender, EventArgs e)
		{
			this.output.Text += "5";
		}

		private void bt6_Click(object sender, EventArgs e)
		{
			this.output.Text += "6";
		}

		private void bt8_Click(object sender, EventArgs e)
		{
			this.output.Text += "8";
		}

		private void bt9_Click(object sender, EventArgs e)
		{
			this.output.Text += "9";
		}

		private void bt0_Click(object sender, EventArgs e)
		{
			this.output.Text += "0";
		}

		private void btplus_Click(object sender, EventArgs e)
		{
			
				if (output.Text == "")
				{
					return;
				}
				else
				{
					plus = true;
					output.Tag = output.Text;
					output.Text = "";
				}}
			private void btminus_Click(object sender, EventArgs e)
		{
			if (output.Text == "")
			{
				return;
			}
			else
			{
				minus = true;
				output.Tag = output.Text;
				output.Text = "";
		}

		}

		private void btequal_Click(object sender, EventArgs e)
		{
			equal = true;
			if (plus)
			{
				decimal dec = Convert.ToDecimal(output.Tag) + Convert.ToDecimal(output.Text);
				output.Text = dec.ToString();
			}
			else if (minus)
			{
				decimal dec = Convert.ToDecimal(output.Tag) - Convert.ToDecimal(output.Text);
				output.Text = dec.ToString();
			}
		}

		






	}
}


now I am new to the try catch clause, and I need to add some specifically to catch non -integral numbers and non-number characters

now what exactly does a catch clause do, like specifically where should I put it, at the end right? and how should the code look give or take, just some basics to help me finish up this calculator

thanks guys

Is This A Good Question/Topic? 0
  • +

Replies To: Try- catch clauses with C# calculator help

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Try- catch clauses with C# calculator help

Posted 09 November 2009 - 10:11 AM

1. if you are only allowing the user to add numbers using the buttons, how can there be non-number characters?

2. as for the try/catch... http://lmgtfy.com/?q=c%23+try+catch
Was This Post Helpful? 0
  • +
  • -

#3 jmartin51  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 25-September 09

Re: Try- catch clauses with C# calculator help

Posted 09 November 2009 - 10:16 AM

well the thing is, in my code the user can still input the numbers by clicking on the text box and manually put them in, that includes numbers and characters, so if they add lets say the letter A it has to tell them error only numbers

I googled try catch, was just wondering what kind of code I put in to specifically catch letters

and thanks
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Try- catch clauses with C# calculator help

Posted 09 November 2009 - 10:21 AM

View Postjmartin51, on 9 Nov, 2009 - 01:16 PM, said:

well the thing is, in my code the user can still input the numbers by clicking on the text box and manually put them in, that includes numbers and characters, so if they add lets say the letter A it has to tell them error only numbers

I googled try catch, was just wondering what kind of code I put in to specifically catch letters

and thanks


personally, I would add code to the TextChanged event for the textbox to just not allow non-numeric characters. This would make it so that you know there are only numbers in the textbox.

// 47-57 are 0-9, 8 is backspace
if ((e.KeyChar < 47 || e.KeyChar > 57) && e.KeyChar != 8)
{
    e.Handled = true;
}



In my opinion, it's better to restrict the user to only allow them to do what YOU want them to do, rather than let them do whatever they want and you try to catch everything.

This post has been edited by eclipsed4utoo: 09 November 2009 - 10:28 AM

Was This Post Helpful? 0
  • +
  • -

#5 jmartin51  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 25-September 09

Re: Try- catch clauses with C# calculator help

Posted 09 November 2009 - 10:26 AM

I totally get you but for this project I have to put in a try catch clause and I can not figure out what the code would be

try
{what code means if a character was added
}

catch
{
this.output.text = error only use numbers
}


it's the try code that I cant figure out
Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Try- catch clauses with C# calculator help

Posted 09 November 2009 - 10:28 AM

View Postjmartin51, on 9 Nov, 2009 - 01:26 PM, said:

I totally get you but for this project I have to put in a try catch clause and I can not figure out what the code would be

try
{what code means if a character was added
}

catch
{
this.output.text = error only use numbers
}


it's the try code that I cant figure out


that's basically it. Just put all of your code in the TRY, and in the CATCH, show an error message or whatever you want to do to show an error occured.
Was This Post Helpful? 0
  • +
  • -

#7 jmartin51  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 25-September 09

Re: Try- catch clauses with C# calculator help

Posted 09 November 2009 - 10:39 AM

ok but how do i specifically catch the letters, thats code im having trouble with

or anything that isnt int

This post has been edited by jmartin51: 09 November 2009 - 10:39 AM

Was This Post Helpful? 0
  • +
  • -

#8 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Try- catch clauses with C# calculator help

Posted 09 November 2009 - 10:43 AM

View Postjmartin51, on 9 Nov, 2009 - 01:39 PM, said:

ok but how do i specifically catch the letters, thats code im having trouble with

or anything that isnt int


You don't have to catch those individually. In your "btequals" event, you try to convert the text to a decimal. If it contains letters, it will throw an exception. that's where the CATCH comes in. It "catches" exceptions when they are thrown to allow you to deal with them. So when the CATCH block is executed, you know that the value contained a character. So you simply display some type of error to let the user know.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1