1

It seems that this particular error has been solved quite a few times but my code snippet has something different in that it will never cause an "unassigned" error.

This code is from a project that I am doing for school. I am allowed to ask for help which is what I am hoping to find here. I don't care to mask any of the variables or whatever as it is not for commercial purposes.

This is the error at compile time: "Use of unassigned local variable 'dateStartedActual'"

switch (userType)
{
    case "Doctor":
        string qualification = Microsoft.VisualBasic.Interaction.InputBox("What is the highest qualification this person has", "Qualification", "", -1, -1);
        while (dateStarted == "")
        {
            try
            {
                dateStarted = Microsoft.VisualBasic.Interaction.InputBox("On which date did this person start", "Date Started", "", -1, -1);
                int day = Convert.ToInt32(Regex.Match(dateStarted, @"\d{2}").Value);
                dateStarted.Remove(0,3);
                int month = Convert.ToInt32(Regex.Match(dateStarted, @"\d{2}").Value);
                dateStarted.Remove(0,3);
                int year = Convert.ToInt32(Regex.Match(dateStarted, @"\d{4}").Value);
                dateStartedActual = new DateTime(day, month, year);
            }
            catch (Exception ex)
            {
                MessageBox.Show("The date entered is not valid");
                dateStarted = "";
            }
        }
        string field = Microsoft.VisualBasic.Interaction.InputBox("In which field does this person practice", "Field", "", -1, -1);
        CreateDoctor(qualification, dateStartedActual, field);
        break;
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Plendor
  • 310
  • 1
  • 4
  • 16

2 Answers2

2

There are 2 ways to solve this error.

First

Assign some value of dateStartedActual in catch block.

OR

Second

Provide some default value of dateStartedActual before try block. In that case if there is any exception in try block, your dateStartedActual will have a default value that you provided.

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
  • That seems like a good way of solving the error but I would rather have the inputBox ask the user to fill in the correct date rather than entering default values into my database – Plendor Nov 02 '13 at 08:39
  • @Aernor: You've missed the point - you're already looping round, so the default value won't actually be used. But I still think this is an ugly approach - I'd definitely extract it to a different method. – Jon Skeet Nov 02 '13 at 08:40
  • I'd like to vote this answer up as it is forming part of my solution but I'm new to this forum – Plendor Nov 02 '13 at 08:46
1

my code snippet has something different in that it will never cause an "unassigned" error

Well it clearly does cause that error, which is why you asked the question, no?

Even though you know that any time the exception is thrown, you'll go round the loop again, the compiler doesn't know that... hence the error. The value is not definitely assigned. Of course, you could just give it a dummy value to start with - but personally I don't like doing that.

You'd be better off extracting the parsing code into a separate method, which could look something like:

static DateTime RequestStartDate()
{
    while (true)
    {
        try
        {
            // Ask for date and parse it
            // ...
            return parsedDate;
        }
        catch (Exception e) // See below...
        {
            MessageBox.Show("The date entered is not valid");
        }
    }
}

That method will definitely return a DateTime eventually, or keep looping forever - so any variable assigned by calling that method will be definitely assigned.

Then in your main code you can write:

switch (userType)
{
    case "Doctor":
        string qualification = Microsoft.VisualBasic.Interaction.InputBox("What is the highest qualification this person has", "Qualification", "", -1, -1);
        DateTime dateStarted = RequestStartDate();
        string field = Microsoft.VisualBasic.Interaction.InputBox("In which field does this person practice", "Field", "", -1, -1);
        CreateDoctor(qualification, dateStarted, field);
        break;

As an aside, you're calling string.Remove and ignoring the result - always a bad idea. And parsing the date manually is needlessly complicated - use DateTime.TryParseExact.

Additionally, catching Exception is usually a bad idea - you should catch specific exceptions... although if you use DateTime.TryParseExact you won't need to catch anything, as it will just return false if the value can't be parsed.

I'd also advise you to at least have a using directive for Microsoft.VisualBasic so that you can just use:

string qualification = Interaction.InputBox(...);

etc instead of having a hugely long line each time.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • It's in a while loop that checks if the dateStarted == to "" which is what it will be set to in the catch. So the while wil start the try catch block from the top again. It will never reach the CreateDoctor if the date isn't correct – Plendor Nov 02 '13 at 08:34
  • Thank you I will try that. Thank you for the prompt response – Plendor Nov 02 '13 at 08:43