• We just launched and are currently in beta. Join us as we build and grow the community.

Simple Calculator using a Class in C#

an0ther23

Crypto Market Forecaster
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
84
Likes
179
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
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:

calcdesign.png


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. namespace

    MDAS
  11. {
  12. public

    class

    calculate
  13. {
  14. //declare a private variable as a double
  15. private

    double

    _num1;
  16. private

    double

    _num2;
  17. private

    double

    _total;

  18. //set a readonly property for the total of the MDAS
  19. public

    double

    total
  20. {
  21. get
  22. {
  23. return

    _total;

    //return the total
  24. }
  25. }
  26. //create a property for a private variable so that you can access it
  27. public

    double

    num1
  28. {
  29. get
  30. {
  31. return

    _num1;

    //return the first value
  32. }
  33. set
  34. {
  35. _num1 =

    value

    ;

    //set the first value
  36. }
  37. }
  38. public

    double

    num2
  39. {
  40. get
  41. {
  42. return

    _num2;
  43. }
  44. set
  45. {
  46. _num2 =

    value

    ;
  47. }
  48. }

  49. //create the sub procedures of the MDAS
  50. public

    void

    multiply(

    )
  51. {
  52. //formula of multiplication
  53. _total =

    num1 *

    num2;
  54. }
  55. public

    void

    divide(

    )
  56. {
  57. //formula of division
  58. _total =

    num1 /

    num2;
  59. }
  60. public

    void

    add

    (

    )
  61. {
  62. //formula of addition
  63. _total =

    num1 +

    num2;
  64. }
  65. public

    void

    subtract(

    )
  66. {
  67. //formula of subtraction
  68. _total =

    num1 -

    num2;
  69. }


  70. }

  71. }

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. namespace

    MDAS
  11. {
  12. public

    partial

    class

    Form1
  13. {
  14. public

    Form1(

    )
  15. {
  16. InitializeComponent(

    )

    ;
  17. }

  18. //create a sub procedure for the events of clicking the radio button that handles all of it.
  19. public

    void

    RadioButton_Click(

    object

    sender, System

    .

    EventArgs

    e)
  20. {

  21. //call a constructor method and return to cal as an instance of a class
  22. calculate cal =

    new

    calculate(

    )

    ;

  23. //declaring the string variable represent as a textbox
  24. string

    txtnum1 =

    TextBox1.

    Text

    ;
  25. string

    txtnum2 =

    TextBox2.

    Text

    ;
  26. //declaring the double variable
  27. double

    dbl_val1 =

    default

    (

    double

    )

    ;
  28. double

    dbl_val2 =

    default

    (

    double

    )

    ;

  29. if

    (

    Information.

    IsNumeric

    (

    txtnum1)

    &&

    Information.

    IsNumeric

    (

    txtnum2)

    )

    //check if the textbox has a numeric value
  30. {
  31. //convert the string to double
  32. dbl_val1 =

    double

    .

    Parse

    (

    txtnum1)

    ;
  33. dbl_val2 =

    double

    .

    Parse

    (

    txtnum2)

    ;

  34. //get the value of the converted variable
  35. //to pass it into the variable in the class
  36. cal.

    num1

    =

    dbl_val1;
  37. cal.

    num2

    =

    dbl_val2;

  38. //the condition is, if the radiobutton is clicked,
  39. //the operation of MDAS executes.
  40. if

    (

    Radio_Multiplication.

    Checked

    )
  41. {
  42. //result:
  43. cal.

    multiply

    (

    )

    ;

    //call a subname in a class for multiplying
  44. }
  45. else

    if

    (

    Radio_Division.

    Checked

    )
  46. {
  47. //result:
  48. cal.

    divide

    (

    )

    ;

    //call a subname in a class for dividing
  49. }
  50. else

    if

    (

    Radio_Addition.

    Checked

    )
  51. {
  52. //result:
  53. cal.

    add

    (

    )

    ;

    //call a subname in a class for adding
  54. }
  55. else

    if

    (

    Radio_Subtaction.

    Checked

    )
  56. {
  57. //result:
  58. cal.

    subtract

    (

    )

    ;

    //call a subname in a class for subtracting
  59. }
  60. }
  61. else
  62. {
  63. //the result is:
  64. //if the textbox is empty or has a string value
  65. TextBox3.

    Text

    =

    "Enter a number"

    ;
  66. return

    ;
  67. }
  68. //put the result of the MDAS to a textbox.
  69. TextBox3.

    Text

    =

    cal.

    total

    .

    ToString

    (

    )

    ;
  70. }


  71. }

  72. }

Output:
calcout.png


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 Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer

If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

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


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,292

324,360

324,368

Top