Search code examples
c#scopeunassigned-variable

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?


Solution

  • 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;
            }
        }
    
    
    }