Search code examples
c#instanceutf-16console.writeline

Is there another alternate way to add Console.WriteLine() in this scenario


In this code, I wish to add a Console.Writeline() after each Console.ReadLine() but how I am implementing the code it is giving me an error. Is there another way how I can add Console.WriteLine() in the instance?

     public void CreateAccount()
        {
        Console.WriteLine("-=-=-=-=-=-=-=-=-=-=-=-=-=-");
        Console.WriteLine("Create an Account");
        Client createAccount = new Client("Create")
        {
            NameOfUser = Console.ReadLine(),
            SurnameOfUser = Console.ReadLine(),
            UserID = Console.ReadLine(),
            UserEmail = Console.ReadLine(),
            UserHomeAdd = Console.ReadLine(),
            UserMobileNumber = int.Parse(Console.ReadLine()),
            UsernameField = Console.ReadLine(),
            PasswordField = Console.ReadLine(),
            CoffePoints = int.Parse(Console.ReadLine())

        };


        List<Client> accountData = new List<Client>()
        {
            createAccount
        };

Solution

  • You can't put WriteLine() between your ReadLine(), because you're initializing properties of your new Client. You can, however, do it like this instead:

    public void CreateAccount()
    {
        Console.WriteLine("-=-=-=-=-=-=-=-=-=-=-=-=-=-");
        Console.WriteLine("Create an Account");
        Client createAccount = new Client("Create");
    
        Console.WriteLine("Enter NameOfUser ");
        createAccount.NameOfUser = Console.ReadLine();
    
        Console.WriteLine("Enter SurnameOfUser ");
        createAccount.SurnameOfUser = Console.ReadLine();
    
        Console.WriteLine("Enter UserID ");
        createAccount.UserID = Console.ReadLine();
    
        Console.WriteLine("Enter UserEmail ");
        createAccount.UserEmail = Console.ReadLine();
    
        Console.WriteLine("Enter UserHomeAdd ");
        createAccount.UserHomeAdd = Console.ReadLine();
    
        Console.WriteLine("Enter UserMobileNumber ");
        createAccount.UserMobileNumber = int.Parse(Console.ReadLine());
    
        Console.WriteLine("Enter UsernameField ");
        createAccount.UsernameField = Console.ReadLine();
    
        Console.WriteLine("Enter PasswordField ");
        createAccount.PasswordField = Console.ReadLine();
    
        Console.WriteLine("Enter CoffePoints ");
        createAccount.CoffePoints = int.Parse(Console.ReadLine());
    
    
        List<Client> accountData = new List<Client>()
        {
            createAccount
        };
    

    When you appreciate why this works, I'd recommend to do like Isma suggested (if you've been taught about how to make your own methods yet), to make your code cleaner. I wrote this to help you appreciate why what you wrote wasn't working out. Shorthand property initializers like this:

    Something s = new Something(){
       Property1 = ReadLine(),  //no semicolon here, this is all
       Property2 = ReadLine()   //one line of code in a=1,b=2,c=3 pattern
    };
    

    Cannot have multiple lines of code like this:

    Something s = new Something(){
       Property1 = WriteLine("Blah"); ReadLine(); //can't put a semicolon here
       Property2 = WriteLine("Blah"); ReadLine(); //it HAS to be a comma, because it
       Property3 = WriteLine("Blah"); ReadLine(); //HAS to be a single line of code
    };
    

    Remember that it is not the return key that defines a new line of code in C#, it's the semicolon. It's simply a language rule that the pattern for setting properties like this is single line, and only one statement can appear on the right hand side of the =

    You must either not use the shorthand way (as above) or you must put all the multiple lines of code you want to use into a single method, and then call that method (as Isma suggested)

    I'd also like to point out that you said you wanted to "writeline a message after every readline" - note that your program will wait for the user to input anything before it prints your message. Isma's way (and this above) print a message BEFORE asking for a readline, because this is more typically what you'd want to do.

    If you really do want it after, then move them to be after (but I guess really you can only be thanking them for their input, and overly thankful things are annoying...) so something like this (Isma's way):

    private static string ReadLine(string writeMessage)
    {
        string s = Console.ReadLine();
        Console.WriteLine(writeMessage);
        return s;
    }
    

    or my way:

    public void CreateAccount()
    {
        Console.WriteLine("-=-=-=-=-=-=-=-=-=-=-=-=-=-");
        Console.WriteLine("Create an Account");
        Client createAccount = new Client("Create");
    
        createAccount.NameOfUser = Console.ReadLine();
        Console.WriteLine("Thanks for entering NameOfUser..");