Developing applications that use system types and collections

These questions are based on 70-622CSHP – TS: Microsoft .NET Framework 2.0 – Application Development Foundation (C#.NET) Microsoft Self-Test Software Practice Test.

 

Objective: Developing applications that use system types and collections
SubObjective: Control interactions between .NET Framework application components by using events and delegates (Refer System namespace)

 

Item No. 70-536CSHP.1.6.2
Single Answer, Multiple Choice

 

You are an application developer for a company. You are creating a class named CalcSalary to calculate the salaries of company employees. This class also contains methods to increment and decrement employee salaries. The CalcSalary class contains the following code:

 

public class CalcSalary {
//for promotions
public static bool IncrementSalary ( Employee Emp, double Amount ) {
if ( Emp.Status == QuarterlyReview.AboveGoals ) {
Emp.Salary += Amount;
return true;
} else
return false;
}
//for demotions
public static bool DecrementSalary ( Employee Emp, double Amount ) {
if ( Emp.Status == QuarterlyReview.BelowGoals ) {
Emp.Salary -= Amount;
return true;
} else
return false;
}
}

 

You want to invoke the IncrementSalary and DecrementSalary methods dynamically at runtime from the sales manager application. You decide to create a delegate named SalaryDelegate to invoke these two methods.

 

Which code should you use to declare the SalaryDelegate delegate?

 

 

  1. public bool Salary (Employee Emp, double Amount);
  2. public delegate void Salary (Employee Emp, double Amount);
  3. public delegate bool Salary (Employee Emp, double Amount);
  4. public event bool Salary (Employee Emp, double Amount);

 

Answer:

 

 

  1. public delegate bool Salary (Employee Emp, double Amount);

 

Tutorial:
You should use the following code to declare the SalaryDelegate delegate:

 

public delegate bool Salary (Employee Emp, double Amount);

 

The signatures of the delegate and the attached method(s) should be identical. When you declare a delegate, you use the delegate keyword followed by the return type. If you bind the delegate to a method with a return type, you should specify that. If you bind the delegate to a method that does not return a data type, you should use the void keyword. After that, you should specify the name of the delegate and declare the arguments expected.

 

In this scenario, the IncrementSalary and DecrementSalary methods accept an Employee object and a double value and return a boolean value. Therefore, when you declare the SalaryDelegate delegate, you should accept an Employee object and double value and return a boolean value.

 

You should not use the code that does not use the delegate keyword. When you declare a delegate, you use the delegate keyword followed by the return data type.

 

You should not use the code that uses the void keyword because both the IncrementSalary and DecrementSalary methods return a boolean value.

 

You should not use the code that declares an event named SalaryDelegate. Although delegates are used in event-handling, the source is responsible for firing the event. The CalcSalary class must contain the event declaration and event firing code. The CalcSalary class does not contain this code, so you should declare a delegate instead of an event.

 

Reference:
MSDN2 Library > .NET Development > .NET Framework SDK > Class Library Reference > System > Delegate Class

 

MSDN2 Library > .NET Development > .NET Framework SDK > .NET Framework > Programming with the .NET Framework > Handling and Raising Events

Like what you see? Share it.Share on Google+Share on LinkedInShare on FacebookShare on RedditTweet about this on TwitterEmail this to someone
CertMag Staff

ABOUT THE AUTHOR

Certification Magazine was launched in 1999 and remained in print until mid-2008. Publication was restarted on a quarterly basis in February 2014.

Posted in Archive|

Comment:

Leave a comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>