So I have this class called Employee that stores the employee's username and password.
class Employee
{
string username;
string password;
public string Username
{
get { return username; }
set { username = value; }
}
public string Password
{
get { return password; }
set { password = value; }
}
public Employee(string username, string password)
{
this.username = username;
this.password = password;
}
In my main class, I created an instance of Employee with some sample data.
Employee employee1 = new Employee("John", "123");
I included this piece of code in the main method to authenticate the user based on his/her username and password.
do
{
Console.Write("Username: ");
username = Console.ReadLine();
Console.Write("Password: ");
password = Console.ReadLine();
if (employee1.Username != username || employee1.Password != password)
{
Console.WriteLine("Incorrect Username or Password. Please Try again \n");
}
}
while (employee1.Username != username || employee1.Password != password);
Console.Clear();
Console.Write("Login Sucessful!. Press any key to continue...");
Console.ReadKey();
Instead of having this chunk of code in my main class, I want to transfer this code in Employee.cs that acts as a method, and can be called from the main class. My problem is, how can I access the object from the main class, and get the initialized username and password to apply the same concept on Employee.cs.
Do you mean something like:
class Employee
{
// Existing code above this
public bool Authenticate()
{
var authed = false;
do
{
Console.Write("Username: ");
var attemptUsername = Console.ReadLine();
Console.Write("Password: ");
var attemptPassword = Console.ReadLine();
if (username != attemptUsername || password != attemptPassword)
{
Console.WriteLine("Incorrect Username or Password. Please Try again \n");
}
else
{
authed = true;
}
}
while (!authed);
Console.Clear();
Console.Write("Login Sucessful!. Press any key to continue...");
Console.ReadKey();
return authed;
}
}
Then you can go:
var john = new Employee("John", 123);
john.Authenticate(); // Waits until he enters valid password (Might want to add a max attempts to the Authenticate function and actually use the return value
NOTE: Depending on what framework etc you are using, it's normally a good idea to keep your Employee / Veternerian
classes as POCO's and use services to add logic based around them.
Is this you looking for ?
class Mainclass
{
Main()
{
Employee employee1 = new Employee("John", "123");
employee1.yourMethod();
}
}
public class Employee
{
string username;
string password;
.
.
.
public void yourMethod()
{
do
{
Console.Write("Username: ");
username = Console.ReadLine();
Console.Write("Password: ");
password = Console.ReadLine();
if (veterinarian1.Username != username || veterinarian1.Password != password)
{
Console.WriteLine("Incorrect Username or Password. Please Try again \n");
}
}
while (veterinarian1.Username != username || veterinarian1.Password != password);
Console.Clear();
Console.Write("Login Sucessful!. Press any key to continue...");
Console.ReadKey();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.