Reputation: 13
Here is the full code (skip it if you don't need it):
using System;
namespace Laboratory_work
{
class Program
{
//some code
public class Engineer: WhiteCollarWorker, Method1, Method2
{
public int YearExperience{get; set;}
public bool BachelorDegree{get; set;}
public bool MasterDegree{get; set;}
public override string InformationOutput(Person obj)
{
return base.InformationOutput(obj)+"Experience — "+YearExperience+" year(s)\n"+
"Bachelor Degree — "+BachelorDegree+"\n"+
"Master Degree — "+MasterDegree+"\n";
}
public int WorkOffer()
{
if (Age>=21 && YearExperience>=1 && BachelorDegree==true)
{
Console.WriteLine("\nYou may be offered a job.");
return 1;
}
else
{
Console.WriteLine("\nUnfortunately, there is no job for you.");
return 0;
}
}
public int Invitation()
{
if(WorkOffer()==1)
{
Console.WriteLine("Dear "+Name+" "+Surname+", soon you will receive\n"+
"an email with date and time of your interview\n"+
"at out company.Thank you for your CV.");
return 1;
}
else
{
Console.WriteLine("Dear "+Name+" "+Surname+", unfortunately, you are\n"+
"not suitable for this position. Than you for your CV.\n");
return 0;
}
}
}
//some code
public static void Main()
{
const int N=10;
int Count=0;
var persons1=new Engineer[N];
for (int i=0; i<10; i++)
{
persons1[i] = new Engineer();
Delegate DELnum1=persons1[i].WorkOffer;
Delegate DELnum2=persons1[i].Invitation;
Delegate DDEL=DELnum1+DELnum2;
Console.WriteLine("\n\nENGINEERS\n________\n");
Console.WriteLine("Input information of a person");
Console.Write("Name: ");
persons1[i].Name=Console.ReadLine();
Console.Write("Surname: ");
persons1[i].Surname=Console.ReadLine();
Console.Write("Age: ");
persons1[i].Age=int.Parse(Console.ReadLine());
Console.Write("Occupation: ");
persons1[i].Occupation=Console.ReadLine();
Console.Write("Computer (true or false): ");
persons1[i].ComputerAvailable=bool.Parse(Console.ReadLine());
Console.Write("Experience (in years): ");
persons1[i].YearExperience=int.Parse(Console.ReadLine());
Console.Write("Bachelor Degree (true or false): ");
persons1[i].BachelorDegree=bool.Parse(Console.ReadLine());
Console.Write("Master Degree (true or false): ");
persons1[i].MasterDegree=bool.Parse(Console.ReadLine());
DDEL();
Count++;
Console.WriteLine("Do you want to continue? (Print 'OK' or 'NO'.)");
string answer;
answer=Console.ReadLine();
if (answer=="OK"||answer=="ok") continue;
else break;
}
for(int i=0; i<Count;i++)
{
Delegate DELnum2=persons1[i].Invitation;
Console.WriteLine("\n\n#"+(i+1));
Console.WriteLine(persons1[i].InformationOutput(persons1[i]));
if (DELnum2()==1) Console.WriteLine("\nAccepted for work.");
else Console.WriteLine("\nNot accepted for work.");
}
//some code
Console.WriteLine("\nPress any key to continue . . .");
Console.ReadKey(true);
}
}
}
I create instances of delegate and use them to perform two methods at ones. Delegate:
public delegate int Delegate ();
Methods (in class Engineer):
public int WorkOffer()
{
if (Age>=21 && YearExperience>=1 && BachelorDegree==true)
{
Console.WriteLine("\nYou may be offered a job.");
return 1;
}
else
{
Console.WriteLine("\nUnfortunately, there is no job for you.");
return 0;
}
}
public int Invitation()
{
if(WorkOffer()==1)
{
Console.WriteLine("Dear "+Name+" "+Surname+", soon you will receive\n"+
"an email with date and time of your interview\n"+
"at out company.Thank you for your CV.");
return 1;
}
else
{
Console.WriteLine("Dear "+Name+" "+Surname+", unfortunately, you are\n"+
"not suitable for this position. Than you for your CV.\n");
return 0;
}
}
This is how I connect this methods in one delegate (in cycle):
Delegate DELnum1=persons1[i].WorkOffer;
Delegate DELnum2=persons1[i].Invitation;
Delegate DDEL=DELnum1+DELnum2;
And then use them:
DDEL();
But in output I have two messeges 'Unfortunately, there is no job for you'. Why? Can anyone explain it? Output. Excuse me for random input.
Upvotes: 0
Views: 72
Reputation: 749
The Invitation
method calls the WorkOffer
method in the if condition.
This means that DDEL
calls WorkOffer
directly, and also indirectly through Invitation
.
With that in mind, you don't need to call WorkOffer
directly through the delegate
Upvotes: 2
Reputation: 174289
The answer is quite simple:
You are calling WorkOffer
twice. Once in the delegate and once in Invitation
.
Upvotes: 0