CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2016
    Posts
    2

    Question Hi I need to ask a questions.

    Hello, I'm trying to make a user input but I'm getting an error, this is how it looks, am I not using it right, I though string would work but it dose not.
    Code:
    string.UserInput;
                UserInput = Console.ReadLine();
                if (UserInput == "Shutdown")
    Toughs lines or underlined in red.All of the rest of the code is working.

    Code:
    1>------ Build started: Project: ProBlueBox, Configuration: Debug Any CPU ------
    1>c:\users\andrewlane\documents\visual studio 2015\Projects\ProBlueBox\ProBlueBox\Test.cs(13,20,13,29): error CS0117: 'string' does not contain a definition for 'UserInput'
    1>c:\users\andrewlane\documents\visual studio 2015\Projects\ProBlueBox\ProBlueBox\Test.cs(13,13,13,29): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement
    1>c:\users\andrewlane\documents\visual studio 2015\Projects\ProBlueBox\ProBlueBox\Test.cs(14,13,14,22): error CS0103: The name 'UserInput' does not exist in the current context
    1>c:\users\andrewlane\documents\visual studio 2015\Projects\ProBlueBox\ProBlueBox\Test.cs(15,17,15,26): error CS0103: The name 'UserInput' does not exist in the current context
    Build has been canceled.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Hi I need to ask a questions.

    Remove the '.' between string and UserInput. It should be a space.

  3. #3
    Join Date
    Jul 2016
    Posts
    2

    Re: Hi I need to ask a questions.

    Ok thanks you, can I ask another thing
    How would I make this come up as an Invalid Command when you try to type something thats not setup. It runs my code but eveytime I type like help it brings up the help but under it says Invalid Command after everything I type, I'm using Case.

    Code:
    case "echo":
                        try
                        {
                            Console.WriteLine(input.Remove(0, 5));
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("echo: " + ex.Message);
                        }
                        break;
                        }
                    
                        {
                            Console.WriteLine("Invalid Command.");
                        }
                    
                }
            }
        }

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Hi I need to ask a questions.

    You need to show a more complete code snippet.

  5. #5
    Join Date
    Sep 2000
    Location
    Indianapolis
    Posts
    6,754

    Re: Hi I need to ask a questions.

    Quote Originally Posted by amjrl View Post
    Code:
    case "echo":
                        try
                        {
                            Console.WriteLine(input.Remove(0, 5));
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("echo: " + ex.Message);
                        }
                        break;
                  }
                    
                  {
                            Console.WriteLine("Invalid Command.");
                  }
                    
                }
            }
        }
    For a complete answer, Arjay is correct, you need to show more code.

    In looking at what you did provided, it looks like the line printing 'invalid code" is coming after your case (and likely after your switch), which is likely why it always prints. Are you missing a "default:" case in your switch? The { } before and after Console.WriteLine("Invalid Command."); don't appear to be serving any real value.

    EDIT: I looked closer. My original answer couldn't have been right...

    If you successfully read a value, then you don't throw an exception and the code breaks out of the "echo" case. At that point you hit a "}" which likely ends you switch statement. With the switch statement (likely) ended, then the next code is:

    {
    Console.WriteLine("Invalid Command.");
    }

    As such, the "Invalid Command." likely prints regardless of what your switch or case statements are doing. When do you want "Invalid Command" to print? If this is a default case, then you need to add 'default:' as mentioned before my edit.
    Last edited by Brad Jones; July 29th, 2016 at 10:13 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured