OYORF
OYORF

Reputation: 463

Variable Assigned in try...catch - Scope

I have declared a variable before a try... catch and assigned it in the try block. I keep getting an unassigned variable error for "fileDate".

class Something
{
    string fACR = "BAK";
    int numbDays = 5;

    Public static void Main()
    {
        DateTime fileDate;
        try
        {
            fACR = args[0];
            numbDays = int.Parse(args[2]);
            fileDate = DateTime.Parse(args[1]);
        }
        catch (ArgumentException e)
        {
            Console.WriteLine("INVALID COMMAND LINE ARGUMENTS! Follow Format:");
            Console.WriteLine("<farm_acronym> <yyyy-M-d> <# days>");
            Console.WriteLine(e);
        }

        inFileName = "U:/CANSO/Engineering/Farms/" + fACR +
            "/DailyDownloads/";
        switch (fACR)
        {
            case "DEM":
                inFileName = inFileName + "Report_Recombiner_" + fileDate.ToString("yyyy-MM-dd") + 
                    ".csv";
                break;
            default:
                inFileName = inFileName + "REPORT_Recombiner_" + fileDate.ToString("yyyy-M-d") + 
                    ".csv";
                break;
        }
    }
}

I tried using this while declaring:

DateTime fileDate = null;

That doesn't work either (DateTime is not nullable). Any suggestions?

Upvotes: 1

Views: 567

Answers (3)

kaptan
kaptan

Reputation: 3149

Update: If after logging the error to console you need to stop the execution then you can rethrow the exception. But still the code needs more improvements.

Others explained the reason behind the error. But I would suggest you refactor your code and take the try/catch out and write it as a method. But still the following code is not good. I don't like the way fACR and numbDays are defined.

class Something
{
    string fACR = "BAK";
    int numbDays = 5;

    Public static void Main()
    {
        DateTime fileDate = GetFileDate(args);


        inFileName = "U:/CANSO/Engineering/Farms/" + fACR +
            "/DailyDownloads/";
        switch (fACR)
        {
            case "DEM":
                inFileName = inFileName + "Report_Recombiner_" + fileDate.ToString("yyyy-MM-dd") + 
                    ".csv";
                break;
            default:
                inFileName = inFileName + "REPORT_Recombiner_" + fileDate.ToString("yyyy-M-d") + 
                    ".csv";
                break;
        }
    }
    public static DateTime GetFileDate(string[] args)
    {
        try
        {
            fACR = args[0];
            numbDays = int.Parse(args[2]);
            return DateTime.Parse(args[1]);
        }
        catch (Exception e)
        {
            Console.WriteLine("INVALID COMMAND LINE ARGUMENTS! Follow Format:");
            Console.WriteLine("<farm_acronym> <yyyy-M-d> <# days>");
            Console.WriteLine(e);
            throw;
        }
    }


}

Upvotes: 1

neminem
neminem

Reputation: 2698

You could either use a nullable DateTime, "DateTime?" (this means, explicitly, "a DateTime that also allows for a null value"), or probably easier in this case, just assign your DateTime a flag value when you create it, like DateTime fileDate = DateTime.MinValue

Of course, in this case, it seems like you would just want to return in the catch and put fileDate inside the try scope - what good is having the fileDate variable in scope outside the try/catch, if it wasn't initialized properly anyway?

Upvotes: 0

Avner Shahar-Kashtan
Avner Shahar-Kashtan

Reputation: 14700

You have to assign it some value. As you noted, DateTime isn't a reference type, so it can't be null. So just assign it some default value you know is invalid - say, DateTime.MinValue. You can later check for that value to see if it was assigned.

Of course, if DateTime.MinValue is a valid value for you, as are all other values, you should use a more robust way of checking whether an assignment was made - for instance, by using a nullable DateTime?. In general it is frowned upon .NET to use special values to mark invalid data - this is precisely what Nullable types are for.

Upvotes: 0

Related Questions