从另一个表单获取对象

时间:2016-12-11 13:03:27

标签: c# class object

每次我遇到同样的问题。 我想在我的Beerform中导入我已经制作的对象。 当我尝试执行此代码时,我每次都会收到此错误:CS0051 C#不一致的可访问性:参数类型比方法更难访问

这是我的Form1,其中包含消费者类。

namespace GoedeKillerApp

public partial class Form1 : Form
{


    private Database dbconnection;
    private Consumer consumer;

    public Form1()
    {
        InitializeComponent();
        dbconnection = new Database(); 
    }

    private void signupButton_Click(object sender, EventArgs e)
    {
        consumer = new Consumer(nameTextbox.Text, Convert.ToInt32(ageUpDown.Value));

        if (ageUpDown.Value == 0 || nameTextbox.Text == null)
        {
            MessageBox.Show("Voer de gegevens in!");
            if (ageUpDown.Value < 18)
            {
                MessageBox.Show("You aren't allowed to create a account");
            }
        }
        else
        {
            dbconnection.DatabaseInsert("INSERT INTO consumer(Consumer , Age) VALUES('" + consumer.Name + "' , '" + consumer.Age + "' )");
            if (true)
            {
                this.Hide(); 
                BeerForm beerform = new BeerForm(consumer);                    
                consumer.SaveName(consumer.Name);
                beerform.Show();
                MessageBox.Show("Sign Up Succesful");
            }
        }

    }

    private void loginButton_Click(object sender, EventArgs e)
    {
        dbconnection.ReadGegevens();
    }
}

这是我的第二个名为BeerForm的表格。问题是我无法获得表格1中的课程

namespace GoedeKillerApp

public partial class BeerForm : Form
{
    public BeerForm(Consumer consumer)
    {
        InitializeComponent();

    }



    private void BeerForm_Load(object sender, EventArgs e)
    {

    }

    private void addbeerButton_Click(object sender, EventArgs e)
    {

    }
}

2 个答案:

答案 0 :(得分:0)

一切看起来都很好,我猜它是Consumer类的修饰符,所以这就失败了:

BeerForm beerform = new BeerForm(consumer); 

尝试更改

internal/private class Consumer

public class Consumer

答案 1 :(得分:0)

public partial class BeerForm : Form
{
    internal Consumer consumer ;
    public BeerForm(Consumer theconsumer)
    {
        InitializeComponent();
        consumer = theconsumer ;

    }

// then use consumer variable in your BeerForm procedures.