2010-04-23 25 views
0

我有一個相同的命名空間的問題:如何訪問另一種形式的屬性? C#

public partial class frmForm1 : Form // Form1 
     { 
    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; 
      } 
     } 
} 


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

我怎麼能訪問frmForm1屬性(用戶名,密碼)?誰來幫幫我?謝謝!

+1

假設你有你的類設計一個嚴重的問題。 – Alex 2010-04-23 16:05:40

回答

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

但是,您的用戶名列表中必須包含第一個元素。

完整的源代碼:

 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; 
      } 
     } 
相關問題