This is the solution for the challenge titled ChallengeEpicSpiesAssignment. If you’re having trouble completing this challenge, read through this solution to find the area that you need help finishing. Read only as much as you need, and do your best to complete the rest on your own.

Remember that the details for the challenge are found in the epic-spies-business-rules.txt file found in the CS-ASP_019-Challenge folder. The changes we make will be to meet the business rule requirements set out in that document.

Step 1: Setup Default.aspx Page

To begin, create a new Project and call it ChallengeEpicSpiesAssignment. Add a new Web Form to this and call it Default.aspx. Switch to the Design view and add the following text and Server Controls:



Give the Server Controls the following programmatic IDs:

  1. codeNameTextBox

  2. newAssignmentTextBox

  3. previousCalendar

  4. newCalendar

  5. endCalendar

  6. assignButton

  7. resultLabel

Clear the text from the resultLabel and change the text in the assignButton to "Assign Spy".

Step 2: Business Rule 1

The first step to complete is to satisfy the first business rule laid out for us: The calendar controls need to have initialized values. As per the instructions, they should be initialized as follows:

  • previousCalendar – Today’s Date
  • newCalendar – 14 Days from Today
  • endCalendar – 21 Days from Today

In order to initialize these values, go to the Default.aspx.cs page, and type the following code in the Page_Load event:


This will initialize the calendar controls to the desired values, but because it is located in Page_Load, the values will be set every time the page is loaded. In order to only initialize the values the first time the page loads, check to ensure that this is not a PostBack:


This ensures that the values will not be re-initialized when the page is posted back due to the assignButton being pressed.

Step 3: Business Rules 3 and 4

Because business rule 2 affects whether or not the budget is calculated, we will first write the code to perform the budget calculation. The budget is said to be equivalent to $500 per day between endCalendar and newCalendar. In order to perform this calculation, we will create a TimeSpan variable called totalDurationOfAssignment that determines the difference of the two calendars like so:


Next, we will create a new double called totalCost to hold the budget total:


Note: totalDurationOfAssignment is a TimeSpan value, which cannot be converted implicitly to a double. To solve this, the TotalDays property is attached to totalDurationOfAssignment in order to receive a double value that we can multiply by.

Next, we need to add $1,000 to totalCost if the duration of the assignment is greater than 3 weeks. In order to do so, create in if() statement that checks this condition, then increments totalCost:


Finally, we need to display the message to the user in the resultLabel. In order to do this, make use of the String.Format() method we discussed in the previous lesson. This method will take in the Text properties for both TextBoxes as well as the totalCost variable:



Remember that the placeholders in String.Format() are zero-based and correspond to the order in which you pass in the arguments. In this case, codeNameTextBox.Text will correspond with {0}, not {1}.

Save and run the application to see the result of both scenarios:



Step 4: Business Rule 2

Now that we have all the other business rules in place, the last step is to make sure that at least 14 days are between the end of the previous assignment and the start of the new assignment. To do this, create a new TimeSpan variable called timeBetweenAssignments:


Create an if() statement underneath your code in the assignButton_Click Event. Check if the TotalDays property of timeBetweenAssignments is less than 14. If so, we need to (1) set the resultLabel’s text and (2) change the newCalendar’s SelectedDate. The first part can be done using simple string concatenation, but for the calendar we need to create a new DateTime variable called earliestNewAssignmentDate:


This variable will always be equal to the minimum time required between assignments: 14 days. Now, we can set the newCalendar’s date and the resultLabel’s text. Your code should look like this:


Now, save and run the application to see the result:


Note: Notice that the date on newCalendar has changed back to the required 2-week difference.


This completes the solution for the ChallengeEpicSpiesAssignment. With this challenge, you can begin to see the different elements of C# you’ve been learning working together to form useful and customizable software. This challenge has been the most complicated yet, but hopefully you were able to complete most, if not all of it on your own. If you struggled, try solving it on your own again at a later time. The more you try and struggle, the more you will ultimately learn. Great job with this challenge, keep it up!

Lessons in this Course