0% found this document useful (1 vote)
1K views3 pages

Calculator Using Generic Delegate and Event: Laboratory Exercise

This document provides instructions for a laboratory exercise to create a simple calculator application using generic delegates and events in C#. Students will create a CalculatorClass with generic delegate methods for arithmetic operations and events. They will then create a Windows form calculator application that calls the delegate methods and displays results when buttons are clicked. Completing the challenge adds division and multiplication methods and validates the selected operator. The exercise aims to teach students how to declare and use generic delegates, events, and accessors in a program.

Uploaded by

Ruq Baldemor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
1K views3 pages

Calculator Using Generic Delegate and Event: Laboratory Exercise

This document provides instructions for a laboratory exercise to create a simple calculator application using generic delegates and events in C#. Students will create a CalculatorClass with generic delegate methods for arithmetic operations and events. They will then create a Windows form calculator application that calls the delegate methods and displays results when buttons are clicked. Completing the challenge adds division and multiplication methods and validates the selected operator. The exercise aims to teach students how to declare and use generic delegates, events, and accessors in a program.

Uploaded by

Ruq Baldemor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

IT1811

Laboratory Exercise
Calculator Using Generic Delegate and Event
Objectives:

At the end of the exercise, the students should be able to:

 Declare, instantiate, and invoke delegates;


 Use generic delegate data types in a program; and
 Declare events and use event accessors.

Software Requirement:

 Visual Studio IDE 2015 or higher

Procedures:

Instructions:
1. Create a simple calculator for a Windows application. Name the project as CalculatorApplication and
the form as FrmCalculator.

2. Follow the design for the FrmCalculator using the screenshot below. Refer to the table for the
properties for each control.

Windows Forms Controls Properties


Name: txtBoxInput1
TextBox
Font Size: 12
Name: cbOperator
ComboBox Font Name: Consolas
Font Size: 12
Name: txtBoxInput2
TextBox
Font Size: 12
Label Name: lblDisplayTotal
Name: btnEqual
Button Font Name: Consolas
Font Size: 14

01 Laboratory Exercise 2 *Property of STI


Page 1 of 3
IT1811

3. In the ComboBox, add the following arithmetic operators:


Arithmetic Operators
+
-
*
/

4. Create a class named CalculatorClass and declare a generic delegate named Formula above the
name of the class. See sample code below.
public delegate T Information<T>(T arg1);

5. Inside the class, declare the generic delegate's variable and set its data type to double. See sample
code below.
public Information<string> info;

6. After declaring the variable for the generic delegate, create two (2) methods that return the sum and
difference based on the following table:
Method Name Data Type
GetSum Double
GetDifference Double

7. Add an event accessor named CalculateEvent with two (2) methods add and remove. Set a message
in the console just to confirm if the delegate is added or removed.
Example: Console.WriteLine("Added the Delegate");
8. In the frmCalculator class, declare the variable for the CalculatorClass named cal. After declaring,
instantiate it inside the constructor of frmCalculator.

9. Set the two (2) variables where the data types are double with a variable named num1 and num2.

10. Double click the button to create the method for button event automatically.

11. Get the value of txtBoxInput1 for num1 and txtBoxInput2 for num2.
Note: You may encounter an error that says, "Cannot implicitly convert type string to double." If this happen
use Convert.ToDouble() to convert the value in the TextBox.

01 Laboratory Exercise 2 *Property of STI


Page 2 of 3
IT1811

Challenge Exercise:
12. In CalculatorClass, add two (2) return type methods named GetProduct for multiplication and
GetQuotient for division.

13. In the frmCalculator class, write a condition that validates the selected arithmetic operator in the
ComboBox. It should call the event and display the answer in a label.
Example:
cal.CalculateEvent += new Formula<double>(cal.GetSum);
lblDisplayTotal.Text = cal.GetSum(num1,num2).ToString();
cal.CalculateEvent -= new Formula<double>(cal.GetSum);

14. Run the program to check the output.

15. Inform your instructor once you’re done as this will be recorded.

Grading Rubric:
CRITERIA PERFORMANCE INDICATORS POINTS
Correctness The code produces the expected result. 30
Logic The code meets the specifications of the problem. 30
Efficiency The code is concise without sacrificing correctness and logic. 20
Syntax The code adheres to the rules of the programming language. 20
TOTAL 100

01 Laboratory Exercise 2 *Property of STI


Page 3 of 3

You might also like