none
Unassigned local variable with c# RRS feed

  • Question

  • So my problem is pretty simple and I imagine there is a simple solution but since I'm still pretty new to c# and coming from c there are just some things I don't fully understand yet

    I realised that c# doesn't like it when you leave some stuff unassigned and unlike in c you always have to add an extra "else" or "default" at the end of of a statement.

    And so I did..but the program still won't run and still says"use of unassigned local variable 'dayName'", so here it is.

    class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine(getDay(2));


                Console.ReadLine();
            }

            static string getDay(int dayNum)
            {
                string dayName;

                switch (dayNum)
                {
                    case 0: dayName = "Monday"; break;
                    case 1: dayName = "Tuesday"; break;
                    case 2: dayName = "Wednesday"; break;
                    case 3: dayName = "Thursday"; break;
                    case 4: dayName = "Friday"; break;
                    case 5: dayName = "Saturday"; break;
                    case 6: dayName = "Sunday"; break;
                    default: dayName = "Invalid"; break;
                }

                return dayName;
            }

        }

    As I said, pretty simple code, but I have no clue what to do so if someone can help please do.

    Saturday, August 24, 2019 2:19 PM

Answers

  • Thanks to everyone that responded, for the people who were asking I'm using VS 2019, and I think it was just a bug because when I read the replies and went to open VS ant try and fix it the error message was gone. I'm not sure what caused this, but now it works totally fine.
    • Marked as answer by Lionile Sunday, August 25, 2019 9:08 AM
    Sunday, August 25, 2019 9:05 AM

All replies

  • With VS 2015, it compiles, but you should initialize the dayName variable :

    string dayName = null;
    (or "Invalid")


    • Edited by Castorix31 Saturday, August 24, 2019 2:34 PM
    • Proposed as answer by EckiS Sunday, August 25, 2019 9:02 AM
    Saturday, August 24, 2019 2:33 PM
  • Hello,

    Try the following where if dayName has not been assigned a empty string is returned

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.Write("Enter a week number ");
                var result = Console.ReadLine();
    
                Console.WriteLine(int.TryParse(result, out var weekNumber) ? 
                    GetDay(weekNumber) : 
                    $"{result} is invalid entry");
    
                Console.ReadLine();
            }
    
            private static string GetDay(int dayNum)
            {
                string dayName;
    
                switch (dayNum)
                {
                    case 0: dayName = "Monday"; break;
                    case 1: dayName = "Tuesday"; break;
                    case 2: dayName = "Wednesday"; break;
                    case 3: dayName = "Thursday"; break;
                    case 4: dayName = "Friday"; break;
                    case 5: dayName = "Saturday"; break;
                    case 6: dayName = "Sunday"; break;
                    default: dayName = "Invalid"; break;
                }
    
                return string.IsNullOrWhiteSpace(dayName) ? "" : dayName;
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, August 24, 2019 3:58 PM
    Moderator
  • Hi Lionile,

    I'm curious as to what Visual Studio version you're using. This compiles fine in VS 2017 as well as VS 2015, as @Castorix has said. Maybe a problem with earlier versions or a Community version? Just a guess.  

    Or, are you using something like FxCop or some other plug-in that checks for possible syntax errors and other things. If so, you can usually set different levels of warnings/errors with them. I've never used them, though … just the regular VS error-checking is good enough for me.

    Because you *do* have a default in your switch-case, the dayName variable will always be set to something and you should not have to initialize it when you define it. But, depending on the reasons for your compiler error, you just might have to.   =0(


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Sunday, August 25, 2019 1:19 AM
    Moderator
  • The source code you posted (as in https://dotnetfiddle.net/gfOzte) works for me too. Perhaps the code you posted does not match what you are getting the error from.

    Can you post the exact error message? And tell us what line that the error message says has the error?



    Sam Hobbs
    SimpleSamples.Info

    Sunday, August 25, 2019 5:31 AM
  • Thanks to everyone that responded, for the people who were asking I'm using VS 2019, and I think it was just a bug because when I read the replies and went to open VS ant try and fix it the error message was gone. I'm not sure what caused this, but now it works totally fine.
    • Marked as answer by Lionile Sunday, August 25, 2019 9:08 AM
    Sunday, August 25, 2019 9:05 AM
  • Beginners often mark a reply as the answer that says other replies helped or that they solved the problem in a manner suggested by other replies. You are supposed to mark the specific reply or replies that helped. If someone with the same or similar problem looks for help and read the one you have marked as the answer then they will get no help.


    Sam Hobbs
    SimpleSamples.Info

    Sunday, August 25, 2019 4:12 PM