如何从另一个表单访问对象c#

时间:2016-01-02 12:58:08

标签: c#

所以我有一个名为Employee的班级,用于存储员工的用户名和密码。

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

我在main方法中包含了这段代码,用于根据用户的用户名和密码对用户进行身份验证。

 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 个答案:

答案 0 :(得分: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并使用服务添加基于它们的逻辑通常是个好主意。

答案 1 :(得分: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();
        }

        }