-1

hi iam creating a hotel reservation form and wanted to calculate total cost of stay by the nights stayed. it requires an arrival date and departure date but i want to add a validation so if the user inputs an incorrect format a message box displays asking them to try again. here is my code already had a bit of help with converting the timespan so once again any help would be amazing. the error is on the line that begins "dateDiff = aDate" and it says the variables aDate and dDate are unassigned thanks in advance:

        String arrival, departure;
        arrival = textBox1.Text;
        departure = textBox2.Text;

        DateTime aDate, dDate;

        try
        {
            aDate = DateTime.ParseExact(arrival, "dd/mm/yyyy", null);
            dDate = DateTime.ParseExact(departure, "dd/mm/yyyy", null);
            return;
        }
        catch
        {
            MessageBox.Show("Invalid input format please enter in format DD/MM/YYYY");
        }

        TimeSpan dateDiff;
        dateDiff = dDate.Subtract(aDate);
        int nights = (int)dateDiff.TotalDays;

        textBox3.Text = ("" + nights);
        textBox5.Text = ("£" + (nights * 115));
AYK
  • 3,312
  • 1
  • 17
  • 30

5 Answers5

2

The reason for the compiler warning is that you haven't assigned a value to your local DateTime fields. Local variables are not initialized with a default value, hence you must do it manually before you can use them. Since you assign the value in a Try/Catch it's not ensured that they will ever get one.

Instead you could use DateTime.TryParseExact:

DateTime aDate, dDate;
if( DateTime.TryParseExact(arrival, "dd/mm/yyyy", null, DateTimeStyles.None, out aDate)
 && DateTime.TryParseExact(departure, "dd/mm/yyyy", null, DateTimeStyles.None, out dDate))
{
    // ...
}
else{
    MessageBox.Show("Invalid input format please enter in format DD/MM/YYYY");
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

Your code it continuing after your catch. Place the code using the dates within your try-block.

        String arrival, departure;
        arrival = textBox1.Text;
        departure = textBox2.Text;

        DateTime aDate, dDate;

        try
        {
            aDate = DateTime.ParseExact(arrival, "dd/mm/yyyy", null);
            dDate = DateTime.ParseExact(departure, "dd/mm/yyyy", null);

            TimeSpan dateDiff;
            dateDiff = dDate.Subtract(aDate);
            int nights = (int)dateDiff.TotalDays;

            textBox3.Text = ("" + nights);
            textBox5.Text = ("£" + (nights * 115));
        }
        catch
        {
            MessageBox.Show("Invalid input format please enter in format DD/MM/YYYY");
        }

And don't return if they parse successfully, or you'll have no result when your input does validate.

Alternatively, place the return in your catch-block, so that execution is stopped on failure.

René Wolferink
  • 3,558
  • 2
  • 29
  • 43
1

Your code should be

    String arrival, departure;
    arrival = textBox1.Text;
    departure = textBox2.Text;

    DateTime aDate, dDate;

    try
    {
        aDate = DateTime.ParseExact(arrival, "dd/mm/yyyy", null);
        dDate = DateTime.ParseExact(departure, "dd/mm/yyyy", null);

        TimeSpan dateDiff;
        dateDiff = dDate.Subtract(aDate);
        int nights = (int)dateDiff.TotalDays;

        textBox3.Text = ("" + nights);
        textBox5.Text = ("£" + (nights * 115));

    }
    catch
    {
        MessageBox.Show("Invalid input format please enter in format DD/MM/YYYY");
        return;
    }
AYK
  • 3,312
  • 1
  • 17
  • 30
0

Assign some default values to your aDate, dDate, and the error will go away. The reason is that compiler can't determine if they will be assigned values for sure in the try block. You can do

DateTime aDate = default(DateTime);
DateTime dDate = default(DateTime);

BUT

instead of using try-catch to validate date, its better if you use DateTime.TryParseExact

DateTime aDate, dDate;


if (DateTime.TryParseExact(arrival, 
                           "dd/MM/yyyy", 
                            CultureInfo.InvariantCulture,
                            DateTimeStyles.NoCurrentDateDefault, 
                            out aDate))
{
    MessageBox.Show("Invalid input format please enter in format DD/MM/YYYY");
}

So your complete code should be:

String arrival, departure;
arrival = textBox1.Text;
departure = textBox2.Text;

DateTime aDate, dDate;


if (DateTime.TryParseExact(arrival, 
                            "dd/MM/yyyy", 
                            CultureInfo.InvariantCulture, 
                            DateTimeStyles.NoCurrentDateDefault, 
                            out aDate))
{
    MessageBox.Show("Invalid input format for Arrival Date - please enter in format DD/MM/YYYY");
}
if (DateTime.TryParseExact(departure, 
                            "dd/MM/yyyy", 
                            CultureInfo.InvariantCulture, 
                            DateTimeStyles.NoCurrentDateDefault, 
                            out dDate))
{
    MessageBox.Show("Invalid input format for Departure Date - please enter in format DD/MM/YYYY");
}


TimeSpan dateDiff;
dateDiff = dDate.Subtract(aDate);
int nights = (int)dateDiff.TotalDays;

textBox3.Text = ("" + nights);
textBox5.Text = ("£" + (nights * 115));
Habib
  • 219,104
  • 29
  • 407
  • 436
  • thanks for getting back i was reading up on that earlier but went with try and catch as it is something im more familiar with (very new to this) but really looking to make my coding as efficient as possible so ill try to start integrating it in the future – user1823383 Nov 16 '12 at 11:19
  • @user1823383, For data parsing, I would suggest always prefer TryParse methods with respective data types, Exception handling for validation can be really costly and should be avoided if one can. – Habib Nov 16 '12 at 11:20
0

Use dateTimePicker control you don't need to Parse

Obama
  • 2,586
  • 2
  • 30
  • 49