A get or set accessor required (Console Application)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: A get or set accessor required (Console Application)

  1. #1
    Join Date
    May 2012
    Posts
    3

    A get or set accessor required (Console Application)

    Beginner here. I'm stuck on chapter 8 in my book: Nested If Statements.
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main();
               int ID
            { 
                       Console.WriteLine("Enter Username");
                    string Name = Console.ReadLine();
                if (Name.ToLower() == "administrator")
               }
        {
                
                    Console.WriteLine("Welcome System Administrator. Enter your password.");
                    if (Console.ReadLine() == "root")
                    {
                        Console.WriteLine("System Administrator logon successful!");
                    }
                    else
                    {
                        Console.WriteLine("Invalid password. Please log on as guest.");
                    }
                }
                else
                {
                    Console.WriteLine("Welcome System User. Please enter the confirmation code to confirm you are a user of the mainframe.");
                        if (Console.ReadLine() == "ibmmainframe7")
                        {
                            Console.WriteLine("Welcome System User. Please enter your ID");
                    ID = Console.ReadLine();
                    Console.WriteLine("Welcome system user # " ID);
                }
                    else
                {
                    Console.WriteLine("Welcome System Guest!");
                }
    
                }
    
    
             Console.ReadLine();
                    
                    }
            }
        }

  2. #2
    Join Date
    Feb 2012
    Location
    Strasbourg, France
    Posts
    116

    Re: A get or set accessor required (Console Application)

    { and } missing at some places.
    Console.ReadLine()); returns a string, so you need to parse it before putting it inside an integer
    static void Main(); if you put ; here it means that you've finished implementing the function

    Code:
    namespace ConsoleApplication2
    {
        class Program
        {
            
            static void Main()
            {
                int ID;
                Console.WriteLine("Enter Username");
                string Name = Console.ReadLine();
                if (Name.ToLower() == "administrator")
                {
    
                    Console.WriteLine("Welcome System Administrator. Enter your password.");
                    if (Console.ReadLine() == "root")
                    {
                        Console.WriteLine("System Administrator logon successful!");
                    }
                    else
                    {
                        Console.WriteLine("Invalid password. Please log on as guest.");
                    }
                }
                else
                {
                    Console.WriteLine("Welcome System User. Please enter the confirmation code to confirm you are a user of the mainframe.");
                    if (Console.ReadLine() == "ibmmainframe7")
                    {
                        Console.WriteLine("Welcome System User. Please enter your ID");
                        ID = int.Parse(Console.ReadLine());
                        Console.WriteLine("Welcome system user # " + ID.ToString());
                    }
                    else
                    {
                        Console.WriteLine("Welcome System Guest!");
                    }
    
                }
    
    
                Console.ReadLine();
    
            }
        }
    }
    Last edited by Erendar; May 29th, 2012 at 10:55 AM.

  3. #3
    Join Date
    Mar 2001
    Posts
    2,375

    Re: A get or set accessor required (Console Application)

    Well if I read your post correctly, you are asking for a getter or a setter.

    CSharp uses a thing called properties:

    Code:
    private int tries = 1;
    public int Tries
    {
       set
       {
          if (val > MIN_TRIES || val < MAX_TRIES)
           tries = value;
       }
       get
       {
          return tries;
       }
    }
    Properties do exactly the same thing Setters and Getters Do, only
    Microsoft Specs properties over Setters and Getters in CSharp. Setters and Getters are used more in C++ OOP.

    What a setter is is its a public function that passes in a value to set a private member variable. There should be a validation of this value to make sure no crazy value is passed in.

    Code:
    public void SetNumberTries(int nTries)
    {
        if (nTries > MIN_RETRIES || nTries < MAX_RETRIES) 
            tries = nTries;
    }
    Here is a getter.
    Code:
    public int GetNumberTries()
    {
        return tries;
    }
    These functions are used to safely and securely change and retrieve member variables.
    Last edited by ahoodin; May 29th, 2012 at 10:59 AM.
    ahoodin
    To keep the plot moving, that's why.

  4. #4
    Join Date
    Feb 2012
    Location
    Strasbourg, France
    Posts
    116

    Re: A get or set accessor required (Console Application)

    reedited my post correting mistakes

  5. #5
    Join Date
    May 2012
    Posts
    3

    Re: A get or set accessor required (Console Application)

    I meant "expected" not required (typo lol) in the title as in the error. The application was so sloppy that I rewrote it and it works perfect now.

  6. #6
    Join Date
    Mar 2001
    Posts
    2,375

    Re: A get or set accessor required (Console Application)

    Ok Great. Do you want to post your newly written app?
    ahoodin
    To keep the plot moving, that's why.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center