CIS 220 - Program 5
Parameter Passing


Objective: Practice with form-level variables, subs, functions, and value and reference parameters.

Description: Write a date conversion program that reads in the current month, day, and year and converts and stores that data as a Julian date.


Form-level variable:

  • julianDate:  a long variable used to store the Julian version of the date throughout the life of the program.  This variable can be referenced directly in the click events, but if it is needed in any other subs or functions it must be passed as a parameter.

Procedures:

  • convertToJulian
  • getDateComponents
  • validateMonth
  • validateDay
  • validateYear
  • isLeapYear
  • cmdSaveDate_Click
  • cmdDisplaydate_Click

The following code segment can be used (with slight modifications) in a procedure to convert a month, day, and year to its Julian equivalent (convertToJulian):

Dim a As Long = (14 - mm) \ 12
Dim y As Long = yyyy + 4800 - a
Dim m As Long = mm + 12 * a - 3
Dim newJulianDate As Long = dd + ((153 * m + 2) \ 5) + (365 * y) + (y \ 4) - (y \ 100) + (y \ 400) - 32045

The following code segment can be used (with slight modifications) in a procedure to extract the month, day, and year from the Julian equivalent (getDateComponents):

 If (julianDate <> 0) Then
     Dim IJD As Long = Math.Floor(julianDate )
     Dim L As Long = Math.Floor(IJD + 68569)
     Dim N As Long = Math.Floor(4 * L / 146097)
     L = L - Math.Floor((146097 * N + 3) / 4)
     Dim I = Math.Floor(4000 * (L + 1) / 1461001)
     L = L - Math.Floor(1461 * I / 4) + 31
     Dim J As Long = Math.Floor(80 * L / 2447)
     Dim K As Long = L - Math.Floor(2447 * J / 80)
     L = Math.Floor(J / 11)
     J = J + 2 - 12 * L
     I = 100 * (N - 49) + I + L
     newDay = CInt(K)
     newmonth = CInt(J)
     newYear = CInt(I)
Else
     newDay = DateTime.Now.Day
     newmonth = DateTime.Now.Month
     newYear = DateTime.Now.Year
End If

Your task is to package these code segments in either a sub or function (select the best one) and use the proper ByRef or ByVal parameters.


Create an application to test your class.  The interface should resemble the image below:

The Save Date button should (1) validate the input, resetting to the current month, day, or year if the corresponding entry is invalid, (2) convert the date to Julian format (by calling the convertToJulian procedure) and store it in the form-level julianDate variableMake it work as much like the demo as possible.

The Display Date button should convert the Julian date back to its original components (month, day, and year) and display both the components and the Julian version of the date (see the screenshot above).  Make it work as much like the demo as possible.


Code Snippets  (versions of these are used in the sample program)

     Private Sub validateMonth(ByRef mth As Integer)
          mth =  IIf((mth > 0 And mth <= 12), mth, DateTime.Now.Month)
     End Sub

     Private Sub validateDay(ByVal mth As Integer, _
                         ByRef dy As Integer, _
                         ByVal yr As Integer)

          Dim validDay As Integer

         
Dim daysPerMonth( ) As Integer = {0, 31, 28, 31, 30, 31, 30, 31, 31, _
                                                                                30, 31, 30, 31}

          If dy > 0 And dy <= daysPerMonth(mth) Then
               validDay = dy
          ElseIf mth = 2 And dy = 29 And isLeapYear(yr) Then
               validDay = dy
          Else
               ' An invalid day was passed to validateDay
               ' Set the day to a default value of today's date
               validDay  = DateTime.Now.Day
          End If
          dy = validDay
     End Sub

 

     Private Sub validateYear (ByRef yyyy As Integer)
          If yyyy < 1 Or yyyy > 9999 Then
               yyyy = DateTime.Now.Year
          End If
     End Sub
 


Wikipedia entry


Comment everything thoroughly (including the routines that were provided) and turn in a pseudocode algorithm or flowchart.


Notes:

You declare only one variable at the form level, and that is julianDate. It needs to be Long.

The Save Date button reads month, day, and year from the text boxes into local variables, validates them (by calling the validation routines that I provided for you), and then calls the convertToJulian routine (passing it month, day, and year) to convert month, day and year to Julian format. That Julian value is returned to the Save Date event procedure which then stores it in the form-level variable, julianDate. Nothing is displayed by the Save Date button event.

The Display Date event procedure also has local variables for month, day, and year. It passes the Julian date (that you just stored in the form-level variable julianDate) to the getDateComponents routine. That getDateComponents routine returns the month, day and year as calculated from the Julian date to the Display Date event procedure which displays the result in a label as shown in the screenshot.

I provided code for the convertToJulian routine and the getDateComponents routine. I did NOT provide the procedure header for either one. That is your job. You have to decide whether each routine should be a sub or a function. You also have to decide what is passed to each routine and whether each item should be passed ByVal or ByRef.