none
Using String as WriteLine in other codeblock. RRS feed

  • Question

  • I'm new to coding. I'm getting the error "Use of unassigned local variable 'ui'" on "Console.WriteLine(ui);" in row 29. What am I doing wrong? thx

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace meny_uppgift_test
    {
        class Program
        {
            static void Main(string[] args)
            {
                string ui;
                bool loop = true;
                while (loop)
                {
                    Console.WriteLine("1 = write message");
                    Console.WriteLine("2 = display message");
                    int menuC = Convert.ToInt32(Console.ReadLine());
    
                    if (menuC == 1)
                    {
                        Console.WriteLine("Write here: ");
                        ui = Console.ReadLine();
                    }
    
                    else if (menuC == 2)
                    {
                        Console.WriteLine(ui);
                    }
                    else if (menuC == 3)
                    {
                        ui = "";
                    }
                    else
                    {
                        loop = false;
                    }
                }
    
            }
        }
    }
    

    Monday, April 2, 2018 11:47 AM

Answers

  • The error message is self explanatory, you must initialize the variable e.g.

    string ui = "";

    Or

    string ui = string.Empty;

     

    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by luddezs Monday, April 2, 2018 12:10 PM
    Monday, April 2, 2018 11:56 AM
    Moderator

All replies

  • The error message is self explanatory, you must initialize the variable e.g.

    string ui = "";

    Or

    string ui = string.Empty;

     

    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by luddezs Monday, April 2, 2018 12:10 PM
    Monday, April 2, 2018 11:56 AM
    Moderator
  • Karen,

     BravoZulu! Thank you very much! I appreciate your reply content. Thanks :)

    Monday, April 2, 2018 12:10 PM