Visual Studio 2017 C# Create a console application named delegatesAndEvents with
ID: 3875913 • Letter: V
Question
Visual Studio 2017 C#
Create a console application named delegatesAndEvents with the following requirements:
1. Declare a delegate type that takes a string as an argument.
2. Create a function named testFun that takes a string argument name and write the name out to the console.
3. Inside the Main function create an instance of the delegate and initialize it with the function.
4. Invoke the delegate to test it using your name as the argument.
5. Add a lambda expression to the delegate using Multicasting that takes a string argument name and write the name out to the console.
6. Invoke the delegate and observe the results.
7. Create a class named Instructor with the following requirements:
a. Declare a public delegate named InstructorEventHandler().
b. Declare an event of type InstructorEventHandler named travelEvent.
c. Public Boolean property named Traveling.
d. private int property named daysBeforeReturning
e. A getter for daysBeforeReturning that returns the value of the property.
f. A setter for daysBeforeReturning which set its value and a test to see if the Instructor is Traveling and if daysBeforeReturning is equal to one or less. If instructor is Traveling and the number of days before returning is one or less the raise the event named travelEvent.
8. In Main create a new instructor instance named teacher.
9. Subscribe to the travelEvent handler, this will cause the event handler code to be generated for you.
10. Modify the event handler to write to the console "The Instructor will return tomorrow".
11. Set teacher.Traveling to True and teacher.DaysBeforeReturning to 5.
12. Set teacher.Traveling to False and teacher.DaysBeforeReturning to 0.
13. Set teacher.Traveling to True.
14. Set teacher.DaysBeforeReturning to 1.
Explanation / Answer
console application delegatesAndEvents
Public class DelegatesAndEvents
{
public void invokeargue(String name, String house)
{
}
function testFun(String name)
{
System.Console.Writeline("Your Name Is",name);
}
static void main(string[] args)
{
testFun("Kasun");
}
}
-------------------------------------------------------------------------------------------------------------------------------------
Part 2
package com.app.test;
public class Instructor {
int daysBeforeReyurning;
boolean travelling;
public Instructor() {
}
public delegate String InstructorEventHandler(String name)
{
}
public void travelEvent(){
System.out.println("The Instructor will return tomorrow");
}
public boolean isTravelling() {
return travelling;
}
public void setTravelling(boolean travelling) {
this.travelling = travelling;
}
private int daysBeforeReturning() {
}
public int getDaysBeforeReyurning() {
return daysBeforeReyurning;
}
public void setDaysBeforeReyurning(int daysBeforeReyurning) {
this.daysBeforeReyurning = daysBeforeReyurning;
}
public static void main(String[] args)
{
Instructor teacher = new Instructor();
teacher.setDaysBeforeReyurning(5);
teacher.setTravelling(true);
int num = teacher.getDaysBeforeReyurning();
boolean travel = teacher.isTravelling();
if(num<=1 && travel==true){
teacher.travelEvent();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.