Question 1) Application Development Foundation

Posted on
Share on Google+Share on LinkedInShare on FacebookShare on RedditTweet about this on TwitterEmail this to someone

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)

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 Shared Function IncrementSalary(ByVal Emp As Employee, _
ByVal Amount As Double) As Boolean
If Emp.Status = QuarterlyReview.AboveGoals Then
Emp.Salary += Amount
Return True
Else
Return False
End If
End Function
‘For demotions
Public Shared Function DecrementSalary(ByVal Emp As Employee, _
ByVal Amount As Double) As Boolean
If Emp.Status = QuarterlyReview.BelowGoals Then
Emp.Salary -= Amount
Return True
Else
Return False
End If
End Function
End Class

 

Visual Studio generates an error when you use it. 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?

A. Public Delegate SalaryDelegate(ByVal Emp As Employee, _
ByVal Amount As Double) As Boolean
B. Public Delegate Sub SalaryDelegate(ByVal Emp As Employee, _
ByVal Amount As Double)
C. Public Delegate Function SalaryDelegate(ByVal Emp As Employee, _
ByVal Amount As Double) As Boolean
D.Public Event SalaryDelegate(ByVal Emp As Employee, _
ByVal Amount As Double

Answer:
C. Public Delegate Function SalaryDelegate(ByVal Emp As Employee, _
ByVal Amount As Double) As Boolean

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

Public Delegate Function SalaryDelegate(ByVal Emp As Employee, _
ByVal Amount As Double) As Boolean

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 either the Function or the Sub keyword. If you bind the delegate to a function, use the Function keyword. If you bind the delegate to a procedure, use the Sub 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 indicate whether the attached method(s) returns a value or not. When you declare a delegate, you use the Delegate keyword followed by either the Function or the Sub keyword. If you bind the delegate to a function, use the Function keyword. If you bind the delegate to a procedure, use the Sub keyword.

You should not use the code that uses the Sub keyword because both the IncrementSalary and DecrementSalary methods are functions, not procedures.

You should not use the code that declares an event named SalaryDelegate. Though 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

 

Share on Google+Share on LinkedInShare on FacebookShare on RedditTweet about this on TwitterEmail this to someone
cmadmin

ABOUT THE AUTHOR

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>