C#与类共享表单数据
这是一个新手问题。我已经有一段时间没有做过C#编程了,尤其是创建自定义类。我想在我的表单之间共享一些数据,我想创建一个类C#与类共享表单数据,c#,winforms,class,C#,Winforms,Class,这是一个新手问题。我已经有一段时间没有做过C#编程了,尤其是创建自定义类。我想在我的表单之间共享一些数据,我想创建一个类 public class User { public string id; public string name; public User() { } public User(string id, string name) { this.id = id; this.name = nam
public class User
{
public string id;
public string name;
public User()
{
}
public User(string id, string name)
{
this.id = id;
this.name = name;
}
}
类是否必须位于单独的文件(User.cs)中
如果我有以下代码:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
User user1 = new User("abc","cde");
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
}
我应该如何在
Form1
中创建类,以及如何在Form2
中访问它?它们可以在同一个文件中。如果要从Form2中的Form1访问数据,请将Form1作为参数添加到Form2的构造函数中。或者将其作为属性添加到Form2中,并允许对其进行“设置”。它们可以位于同一文件中。如果要从Form2中的Form1访问数据,请将Form1作为参数添加到Form2的构造函数中。或者将其作为属性添加到Form2中,并允许对其进行“设置”。通常,您将类分离到单独的文件中,因此我建议创建一个包含用户
类的User.cs
文件
在Form1
中实例化类的方式很好,但要将类的实例传递到Form2
中,需要将其传递给Form2
的构造函数,如下所示:
Form2 theForm2 = new Form2(user1);
public partial class Form2 : Form
{
private User _user;
public User TheUser
{
get
{
return _user;
}
}
public Form2()
{
InitializeComponent();
}
public Form2(User theUser)
{
_user = theUser;
}
}
注意:要完成上述代码,需要修改Form2
的构造函数,或者在本例中创建非默认构造函数,并创建成员变量来保存用户
类实例,如下所示:
Form2 theForm2 = new Form2(user1);
public partial class Form2 : Form
{
private User _user;
public User TheUser
{
get
{
return _user;
}
}
public Form2()
{
InitializeComponent();
}
public Form2(User theUser)
{
_user = theUser;
}
}
现在您可以使用
Form2
中的User
类,只需引用属性TheUser
通常,您将类分离到单独的文件中,因此我建议创建一个包含User
类的User.cs
文件
在Form1
中实例化类的方式很好,但要将类的实例传递到Form2
中,需要将其传递给Form2
的构造函数,如下所示:
Form2 theForm2 = new Form2(user1);
public partial class Form2 : Form
{
private User _user;
public User TheUser
{
get
{
return _user;
}
}
public Form2()
{
InitializeComponent();
}
public Form2(User theUser)
{
_user = theUser;
}
}
注意:要完成上述代码,需要修改Form2
的构造函数,或者在本例中创建非默认构造函数,并创建成员变量来保存用户
类实例,如下所示:
Form2 theForm2 = new Form2(user1);
public partial class Form2 : Form
{
private User _user;
public User TheUser
{
get
{
return _user;
}
}
public Form2()
{
InitializeComponent();
}
public Form2(User theUser)
{
_user = theUser;
}
}
现在,您可以使用
Form2
中的User
类,只需引用属性TheUser
即可。一种方法是在Form2的构造函数中传递类。如下图所示
public partial class Form1 : Form
{
User user1 = new User("abc","cde");
public Form1()
{
InitializeComponent();
new Form2(user1);
}
}
public partial class Form2 : Form
{
User user1;
public Form2(User u)
{
InitializeComponent();
user1 = u;
}
}
其他方法可以是在form1中公开一个返回User的公共属性。您可以在Form2中访问该属性。有多种方法。一种方法是在Form2的构造函数中传递类。如下图所示
public partial class Form1 : Form
{
User user1 = new User("abc","cde");
public Form1()
{
InitializeComponent();
new Form2(user1);
}
}
public partial class Form2 : Form
{
User user1;
public Form2(User u)
{
InitializeComponent();
user1 = u;
}
}
How should I create the class in Form1 and How should I access it in Form 2?
其他方法可以是在form1中公开一个返回User的公共属性。您可以在Form2中访问该属性
How should I create the class in Form1 and How should I access it in Form 2?
这取决于它的用途。因为您希望在表单之间共享它,所以我假设您希望它的范围是类范围。将其定义为类属性。最好在Form1
的构造函数之上
Does the class have to be in a separate file, User.cs If I had the following code
不必如此,但最佳实践是这样的
How do I share it with Form2?
A.将其作为表单2的构造函数的参数:
public partial class Form2 : Form
{
public User MyUser { get; set; }
public Form2(User user)
{
InitializeComponent();
MyUser = user;
}
}
B.将其作为Form2
的公共属性,并从Form1
var frm2 = new Form2();
frm2.User = user;
这取决于它的用途。因为您希望在表单之间共享它,所以我假设您希望它的范围是类范围。将其定义为类属性。最好在Form1
的构造函数之上
Does the class have to be in a separate file, User.cs If I had the following code
不必如此,但最佳实践是这样的
How do I share it with Form2?
A.将其作为表单2的构造函数的参数:
public partial class Form2 : Form
{
public User MyUser { get; set; }
public Form2(User user)
{
InitializeComponent();
MyUser = user;
}
}
B.将其作为Form2
的公共属性,并从Form1
var frm2 = new Form2();
frm2.User = user;
如何分发这些类和文件完全取决于您。在这个示例中,我让您可以拥有UserHandler.cs、User.cs、Form1.cs和Form2.cs。希望能有帮助
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public static class UserHandler
{
private static List<User> Users = new List<User>();
public static void AddNewUser(User user)
{
Users.Add(user);
}
public static void RemoveUser(User user)
{
Users.Remove(user);
}
public static User GetUserById(int id)
{
if(Users.Exists(x => x.userId == id))
{
return Users.Find(user => user.userId == id);
}
return null;
}
}
public class User
{
public int userId { get; set; }
public string userName { get; set; }
public User(int id, string name)
{
this.userId = id;
this.userName = name;
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var _user = new User(1,"user2252502");
UserHandler.AddNewUser(_user);
MessageBox.Show(UserHandler.GetUserById(1).userName);
Form2 form2 = new Form2();
}
}
public partial class Form2 : Form
{
public Form2()
{
MessageBox.Show(UserHandler.GetUserById(1).userName);
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Windows.Forms;
命名空间Windows窗体应用程序1
{
公共静态类UserHandler
{
私有静态列表用户=新列表();
公共静态void AddNewUser(用户用户)
{
用户。添加(用户);
}
公共静态void RemoveUser(用户用户)
{
用户。删除(用户);
}
公共静态用户GetUserById(int-id)
{
if(Users.Exists(x=>x.userId==id))
{
返回Users.Find(user=>user.userId==id);
}
返回null;
}
}
公共类用户
{
public int userId{get;set;}
公共字符串用户名{get;set;}
公共用户(int-id,字符串名称)
{
this.userId=id;
this.userName=名称;
}
}
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
var_user=新用户(1,“user225502”);
UserHandler.AddNewUser(\u user);
Show(UserHandler.GetUserById(1.userName);
Form2 Form2=新Form2();
}
}
公共部分类表单2:表单
{
公共表格2()
{
Show(UserHandler.GetUserById(1.userName);
}
}
}
您如何分发文件类取决于您自己。在这个示例中,我让您可以拥有UserHandler.cs、User.cs、Form1.cs和Form2.cs。希望能有帮助
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public static class UserHandler
{
private static List<User> Users = new List<User>();
public static void AddNewUser(User user)
{
Users.Add(user);
}
public static void RemoveUser(User user)
{
Users.Remove(user);
}
public static User GetUserById(int id)
{
if(Users.Exists(x => x.userId == id))
{
return Users.Find(user => user.userId == id);
}
return null;
}
}
public class User
{
public int userId { get; set; }
public string userName { get; set; }
public User(int id, string name)
{
this.userId = id;
this.userName = name;
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var _user = new User(1,"user2252502");
UserHandler.AddNewUser(_user);
MessageBox.Show(UserHandler.GetUserById(1).userName);
Form2 form2 = new Form2();
}
}
public partial class Form2 : Form
{
public Form2()
{
MessageBox.Show(UserHandler.GetUserById(1).userName);
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Windows.Forms;
命名空间Windows窗体应用程序1
{
公共静态类UserHandler
{
私有静态列表用户=新列表();
公共静态void AddNewUser(用户用户)
{
用户。添加(用户);
}
公共静态void RemoveUser(用户用户)
{
用户。删除(用户);
}
公共静态用户GetUserById(int-id)
{
if(Users.Exists(x=>x.userId==id))
{