开发者

how toaccess the attributes in another form? c#

开发者 https://www.devze.com 2022-12-27 18:53 出处:网络
I have a problem in a same namespace: public partial class frmForm1 : Form// Form1 { public class Account

I have a problem in a same namespace:

public partial class frmForm1 : Form  // Form1
        {
     public class Account
            {
                public string Username;
                public string Passwo开发者_如何学编程rd;

            }

        public class ListAcc
        {
            public static int count = 0;
            private static List<Account> UserList;
            public static List<Account> Data()
            {
                return UserList;
            }
        }
}


 public partial class frmForm2 : Form  // Form2
    {

        private void button2_Click(object sender, EventArgs e)
        {
            frmForm1.Account A;
            string m = frmForm1.ListAcc<A>.[0].Username; //ERROR
        }
    }

How could i access the attributes (Username, Password...) in frmForm1? Someone help me? Thanks!


string m = frmForm1.ListAcc.Data()[0].Username

But, you must have first element in your username list.

Complete Source code:

       public class Account
       {
           public string Username;
           public string Password;
       }

       public class ListAcc
       {
           public static int count = 0;
           private static List<Account> UserList;
           public static List<Account> Data()
           {
                return UserList;
           }
           ListAcc()
           {
                UserList = new List<Account>();
                UserList.Add(new Account() { Username = "x", Password = "y" });
           }
       }

       public partial class frmForm1 : Form  // Form1
       {
            public static ListAcc;
       }


       public partial class frmForm2 : Form  // Form2
       {

           private void button2_Click(object sender, EventArgs e)
           {
               string m = frmForm1.ListAcc.Data()[0].Username;
           }
       }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号