2016-01-02 91 views
-1

所以我有這個叫Employee的類來存儲員工的用戶名和密碼。如何從另一個窗體訪問對象c#

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; 
    } 

在我的主類中,我創建了一個帶有一些示例數據的Employee實例。

Employee employee1 = new Employee("John", "123"); 

我在主要方法中包含了這段代碼,根據用戶名和密碼對用戶進行身份驗證。

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(); 

而是在我的主類有這個代碼塊的,我想轉讓在Employee.cs這個代碼,作爲一種方法,並且可以從主類中調用。我的問題是,我如何從主類訪問對象,並獲得初始化的用戶名和密碼以在Employee.cs上應用相同的概念。

回答

2

你的意思是這樣的:

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; 
    } 
} 

然後你可以去:

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 

注:根據什麼框架等你正在使用,它通常是一個好主意,讓您的Employee/Veternerian類作爲POCO並使用服務來添加基於它們的邏輯。

2

這是你要找嗎?

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(); 
     } 

     } 
+0

是的,但我無法通過Employee.cs中的該方法訪問employee1。 –

+0

好吧,只需在您的員工聲明'公共班級員工'之前添加公開' –

+0

如果您的問題解決了標記答案 –