Skip to main content

Simple Calculator using a Class in C#


In this C# tutorial, I will teach you how to create a program that will compute sum, difference, quotient, and product as just a simple calculator that uses a class.

Now, let's start this tutorial!

1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application.

2. Next, add one TextBox named TextBox1 for inputting first number, Textbox2 for inputting second number, and Textbox3 for displaying the total.Insert also 4 RadioButton named Radio_Addition for addition operation, Radio_Subtraction for subtraction operation, Radio_Division for division operation and Radio_Multiplication for multiplication operation. You must design your layout like this:

design

3. Create a class named calculate and put this code below.

  1. using System.Diagnostics;
  2. using System;
  3. using System.Xml.Linq;
  4. using System.Windows.Forms;
  5. using System.Collections;
  6. using System.Drawing;
  7. using System.Data;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10.  
  11.  
  12. namespace MDAS
  13. {
  14. public class calculate
  15. {
  16. //declare a private variable as a double
  17. private double _num1;
  18. private double _num2;
  19. private double _total;
  20.  
  21. //set a readonly property for the total of the MDAS
  22. public double total
  23. {
  24. get
  25. {
  26. return _total; //return the total
  27. }
  28. }
  29. //create a property for a private variable so that you can access it
  30. public double num1
  31. {
  32. get
  33. {
  34. return _num1; //return the first value
  35. }
  36. set
  37. {
  38. _num1 = value; //set the first value
  39. }
  40. }
  41. public double num2
  42. {
  43. get
  44. {
  45. return _num2;
  46. }
  47. set
  48. {
  49. _num2 = value;
  50. }
  51. }
  52.  
  53. //create the sub procedures of the MDAS
  54. public void multiply()
  55. {
  56. //formula of multiplication
  57. _total = num1 * num2;
  58. }
  59. public void divide()
  60. {
  61. //formula of division
  62. _total = num1 / num2;
  63. }
  64. public void add()
  65. {
  66. //formula of addition
  67. _total = num1 + num2;
  68. }
  69. public void subtract()
  70. {
  71. //formula of subtraction
  72. _total = num1 - num2;
  73. }
  74.  
  75.  
  76. }
  77.  
  78. }

4. Now, go to your Form and put this code below for the calculator operation.

  1. using System.Diagnostics;
  2. using System;
  3. using System.Xml.Linq;
  4. using System.Windows.Forms;
  5. using System.Collections;
  6. using System.Drawing;
  7. using System.Data;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10.  
  11.  
  12.  
  13. namespace MDAS
  14. {
  15. public partial class Form1
  16. {
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. }
  21.  
  22. //create a sub procedure for the events of clicking the radio button that handles all of it.
  23. public void RadioButton_Click(object sender, System.EventArgs e)
  24. {
  25.  
  26. //call a constructor method and return to cal as an instance of a class
  27. calculate cal = new calculate();
  28.  
  29. //declaring the string variable represent as a textbox
  30. string txtnum1 = TextBox1.Text;
  31. string txtnum2 = TextBox2.Text;
  32. //declaring the double variable
  33. double dbl_val1 = default(double);
  34. double dbl_val2 = default(double);
  35.  
  36. if (Information.IsNumeric(txtnum1) && Information.IsNumeric(txtnum2)) //check if the textbox has a numeric value
  37. {
  38. //convert the string to double
  39. dbl_val1 = double.Parse(txtnum1);
  40. dbl_val2 = double.Parse(txtnum2);
  41.  
  42. //get the value of the converted variable
  43. //to pass it into the variable in the class
  44. cal.num1 = dbl_val1;
  45. cal.num2 = dbl_val2;
  46.  
  47. //the condition is, if the radiobutton is clicked,
  48. //the operation of MDAS executes.
  49. if (Radio_Multiplication.Checked)
  50. {
  51. //result:
  52. cal.multiply(); //call a subname in a class for multiplying
  53. }
  54. else if (Radio_Division.Checked)
  55. {
  56. //result:
  57. cal.divide(); //call a subname in a class for dividing
  58. }
  59. else if (Radio_Addition.Checked)
  60. {
  61. //result:
  62. cal.add(); //call a subname in a class for adding
  63. }
  64. else if (Radio_Subtaction.Checked)
  65. {
  66. //result:
  67. cal.subtract(); //call a subname in a class for subtracting
  68. }
  69. }
  70. else
  71. {
  72. //the result is:
  73. //if the textbox is empty or has a string value
  74. TextBox3.Text = "Enter a number";
  75. return ;
  76. }
  77. //put the result of the MDAS to a textbox.
  78. TextBox3.Text = cal.total.ToString();
  79. }
  80.  
  81.  
  82. }
  83.  
  84. }

Output:

output

For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.

Best Regards,

Engr. Lyndon R. Bermoy
IT Instructor/System Developer/Android Developer
Mobile: 09079373999
Telephone: 826-9296
E-mail:[email protected]

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Subscribe at my YouTube Channel at: Ser Bermz

Visit my website at: www.campcodes.com

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.