开发者

use class in another windows form c#?

开发者 https://www.devze.com 2022-12-28 11:41 出处:网络
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, Password, RePassword, Name, bd, dt, dc;                    
    }

    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)
    {
        ListAcc A;   // error
        string n = A<Account>[0].Usename; // error
        // What sho开发者_JAVA百科uld i do?
    }
}

Someone can help me fix this problem? Thanks a lot!


You've nested the Account and ListAcc class inside the frmForm1 class.

Move them outside of frmForm1's class definition or change it to be frmForm1.ListAcc A;

Also, I'm not sure what you're trying to do here. This wouldn't compile no matter what you do. Are you trying to make ListAcc a generic class?

string n = A<Account>[0].Usename; // error


If you need Account in more than one class maybe it's better not to put in in the frmForm1 but in a separate file. A class inside another class is not a good idea.


public partial class frmForm1 : Form  // Form1
{
     public class Account
     {
         //some code
     }

     public class ListAcc
     {
         //SomeCode
     }
}        

public partial class frmForm2 : Form  // Form2
{        
    private void button2_Click(object sender, EventArgs e)
    {
        //Thats will work
        frmForm1.ListAcc A = new frmForm1.ListAcc();   
        string n = A.Data()[0].Usename;
    }
}


the way your class structure is defined you need to declare the variable like this

frmForm1.ListAcc A;


Move Account class outside the frmForm1 class.

Or you should address your Account type through its "parent" type frmForm1:

frmForm1.Account
0

精彩评论

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