none
CS0161 Not All code paths return a value RRS feed

  • Question

  • Hello all, I'm having an issue with a code block I have created. I'm studying C# in college, so I'm a beginner in this coding world.

    The code blocks function is to replace "e" characters with "*" and "a" characters with "_" in the user's last name after it has been entered however I get error CS0161 in Visual Studio. 

    here is the code in question: 

    static void Main(string[] args)
            {
                string fName, sName;
                fName = getName("first");
                sName = getName("second");
                displayName(fName, sName);
                encryptName(sName);
                pauseProgram();
    
            }//end main
    
            static string encryptName(string sName)
            {
                sName.Replace("e", "*");
                sName.Replace("a", "_");
                Console.WriteLine("Your new username is: {0}", sName);
            }

    Thanks for any help.  
    Saturday, October 26, 2019 12:21 PM

Answers

  • Your method is "static string encryptName (string sName)", that means it is supposed to return a string value, like "return sName;".

    Make it a void method "static void encryptName (string sName)" and the error is gone!

    wizend

    • Marked as answer by whos_bally Saturday, October 26, 2019 12:49 PM
    Saturday, October 26, 2019 12:28 PM

All replies

  • Here you are

            static void encryptName(string sName)
            {
                sName.Replace("e", "*");
                sName.Replace("a", "_");
                Console.WriteLine("Your new username is: {0}", sName);
            }

    Saturday, October 26, 2019 12:27 PM
  • Your method is "static string encryptName (string sName)", that means it is supposed to return a string value, like "return sName;".

    Make it a void method "static void encryptName (string sName)" and the error is gone!

    wizend

    • Marked as answer by whos_bally Saturday, October 26, 2019 12:49 PM
    Saturday, October 26, 2019 12:28 PM
  • Here you are

            static void encryptName(string sName)
            {
                sName.Replace("e", "*");
                sName.Replace("a", "_");
                Console.WriteLine("Your new username is: {0}", sName);
            }



    Thanks!
    • Edited by whos_bally Saturday, October 26, 2019 12:50 PM
    Saturday, October 26, 2019 12:49 PM
  • Thanks!
    Saturday, October 26, 2019 12:50 PM