繁体   English   中英

如何从另一种形式访问对象C#

[英]How to access object from another form 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上应用相同的概念。

您的意思是:

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,并使用服务基于它们添加逻辑。

这是你要找的吗?

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

        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM