You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Form2的personalDetail因保护级别无法访问,跨窗体传值问题排查

问题描述

编写了一个包含Employee类和两个窗体的程序,尝试从Form1向Form2传递数据时,出现错误:'form 2.personal details' is inaccessible due to its protection level。Employee类及其属性均为public。

相关代码

Employee类代码

class Employee
{
    public int EmployeeID { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}

Form1.cs代码

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    Employee personalDetail = new Employee();

    private void btnSetValue_Click(object sender, EventArgs e)
    {
        personalDetail.EmployeeID = Convert.ToInt32(txtEmployeeID.Text);
        personalDetail.Name = txtName.Text;
        personalDetail.Age = Convert.ToInt32(txtAge.Text);

        MessageBox.Show("All data received");
    }

    private void btnSend_Click(object sender, EventArgs e)
    {
        personalDetail.EmployeeID = Convert.ToInt32(txtEmployeeID.Text);
        personalDetail.Name = txtName.Text;
        personalDetail.Age= Convert.ToInt32(txtAge.Text);

        Form2 frm = new Form2();
        frm.personalDetail = personalDetail;

        frm.ShowDialog();
    }
}

Form2.cs代码

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    Employee personalDetail = new Employee();
  
    private void Form2_Load(object sender, EventArgs e)
    {
        textBox1.Text = personalDetail.EmployeeID.ToString();
        textBox2.Text = personalDetail.Name;
        textBox3.Text = personalDetail.Age.ToString();
    }
}

错误原因

Form2中的personalDetail字段未指定访问修饰符,C#中类的字段默认是private级别,外部类(比如Form1)无法直接访问私有成员,这就是报错的核心原因。

解决方案

方法一:将Form2的字段改为public

直接修改Form2的字段访问级别,让Form1可以直接赋值:

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    // 添加public修饰符,开放访问权限
    public Employee personalDetail = new Employee();
  
    private void Form2_Load(object sender, EventArgs e)
    {
        textBox1.Text = personalDetail.EmployeeID.ToString();
        textBox2.Text = personalDetail.Name;
        textBox3.Text = personalDetail.Age.ToString();
    }
}

方法二:通过构造函数传递数据(更推荐)

这种方式符合面向对象的封装原则,避免直接暴露类的内部字段:

  1. 修改Form2,添加带参数的构造函数,用私有字段存储传入的数据:
public partial class Form2 : Form
{
    private Employee _personalDetail;

    // 新增接收Employee对象的构造函数
    public Form2(Employee emp)
    {
        InitializeComponent();
        _personalDetail = emp;
    }
  
    private void Form2_Load(object sender, EventArgs e)
    {
        textBox1.Text = _personalDetail.EmployeeID.ToString();
        textBox2.Text = _personalDetail.Name;
        textBox3.Text = _personalDetail.Age.ToString();
    }
}
  1. 修改Form1的btnSend_Click方法,创建Form2实例时直接传入数据:
private void btnSend_Click(object sender, EventArgs e)
{
    personalDetail.EmployeeID = Convert.ToInt32(txtEmployeeID.Text);
    personalDetail.Name = txtName.Text;
    personalDetail.Age= Convert.ToInt32(txtAge.Text);

    // 通过构造函数传递Employee对象
    Form2 frm = new Form2(personalDetail);
    frm.ShowDialog();
}

额外优化:给Employee类添加public修饰符

当前Employee类默认是internal级别,若后续需要跨程序集访问,建议显式添加public修饰符:

public class Employee
{
    public int EmployeeID { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}

内容的提问来源于stack exchange,提问作者Dhinesh R

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 03:23:42