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();
}
}
}
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 09:55 AM.
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 09:59 AM.
Bookmarks