Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exercise 6 Introduction to C# CIS-2320. 2 Create a class called ParseDates that will parse a formatted string containing a date into separate integers.

Similar presentations


Presentation on theme: "Exercise 6 Introduction to C# CIS-2320. 2 Create a class called ParseDates that will parse a formatted string containing a date into separate integers."— Presentation transcript:

1 Exercise 6 Introduction to C# CIS-2320

2 2 Create a class called ParseDates that will parse a formatted string containing a date into separate integers (these are to be private members) containing the month, day, and year. The constructor is to be passed a char variable indicating how the string is formatted and a string variable containing the date to be parsed. It will then break this string down into a month, day and year. Here are the various options: OptionString formatExample Result Month Day Year 'R'mm/dd/yyyy10/15/2006 10 15 2006 'Y'yyyy/mm/dd2006/01/05 1 5 2006 'A'dd-mmm-yyyy15-NOV-2006 11 15 2006 'F'Mmmmm dd,yyyy Or Mmmmm dd, yyyy June 5,2006 Or June 5, 2006 6 5 2006 'P'mm.dd.yyyy12.31.2006 12 31 2006

3 3 Add these read-only properties to the class: Month- returns the integer member month Day- returns the integer member day Year- returns the integer member yesr

4 4 Next, create a Windows application to test this class. Design a form similar to the following:

5 5 The user will select the format for the date to be entered using a ComboBox and then enter a date in that format.

6 6 When the user presses the tab key, create an instance of the ParseDates class passing the selected option and the date entered to the constructor. When control returns to the application use the ParseDates’s properties to populate the Month, Day, and Year boxes. Then give focus to the Another button. When this button is clicked reset the form to its initial display.

7 7 private void txtDateEntered_Leave(object sender, System.EventArgs e) { char theOne = ' '; switch ( cmbDateFormat.SelectedIndex ) { case 0 : theOne = 'R'; break; case 1 : theOne = 'Y'; break; case 2 : theOne = 'P'; break; case 3 : theOne = 'A'; break; case 4 : theOne = 'F'; break; } ParseDates PD = new ParseDates( theOne, txtDateEntered.Text ); txtMonth.Text = PD.Month.ToString(); txtDay.Text = PD.Day.ToString(); txtYear.Text = PD.Year.ToString(); } private void btnQuit_Click(object sender, System.EventArgs e) { Dispose(); } private void bthAnother_Click(object sender, System.EventArgs e) { txtMonth.Text = ""; txtDay.Text = ""; txtYear.Text = ""; txtDateEntered.Text = ""; cmbDateFormat.SelectedIndex = 0; cmbDateFormat.Focus(); }

8 8 namespace CSExerciseParse { public class ParseDates { private int month; private int day; private int year; private string work; private int i; private string [] mthAbbrev = new string[12] { "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" }; public ParseDates( char format, string strDate ) { work = strDate; string temp = ""; switch (format) { case 'R' : i = work.IndexOf('/'); month = int.Parse( work.Substring(0, i) ); work = work.Remove(0, i+1); i = work.IndexOf('/'); day = int.Parse( work.Substring(0, i) ); work = work.Remove(0, i+1); year = int.Parse( work.Substring(0, work.Length ) ); break; // <========= more code here

9 9 case 'A' : i = work.IndexOf('-'); day = int.Parse( work.Substring(0, i) ); work = work.Remove(0, i+1); i = work.IndexOf('-'); temp = work.Substring(0, i); work = work.Remove(0, i+1); for ( i = 0; i < 12; i++) { if ( temp == mthAbbrev[i] ) break; } month = i + 1; year = int.Parse( work.Substring(0, work.Length ) ); break; default : month = 0; day = 0; year = 0; break; } // end of switch } // end of constructor // <========== properties go here } // end of ParseDates class } // end of namespace


Download ppt "Exercise 6 Introduction to C# CIS-2320. 2 Create a class called ParseDates that will parse a formatted string containing a date into separate integers."

Similar presentations


Ads by Google