c# - 无法从void转换为list

时间:2015-10-03 19:20:38

标签: c# forms list void

所以,当我尝试编写新的东西时,我有点困惑。

我正在尝试将 objectBeer_pluche objectBeer_Elektro 添加到 Beren 主表单上的 OBJberenlijst 详细信息表单,因此我可以将两个类的实例添加到同一个列表中。

顺便说一句,我甚至不确定这是可行的。所以,如果我想做的事情可以从一开始,我想反馈。我已经认为VOID不对,但我真的很无能为力。

这是我的主要 beren.cs 表单,其中包含 OBJberenlist ,我尝试添加 objectBeer_pluche objectBeer_Elektro 进入它:

public partial class Beren : Form
    {
    public interface Berenlijst { }
    public List<Berenlijst> OBJberenLijst = new List<Berenlijst>();
    public Beren()
    {
        InitializeComponent();
    }

    private void Beren_Load(object sender, EventArgs e)
    {

    }

    private void BTNToevoegen_Click(object sender, EventArgs e)
    {
        this.Hide();
        Details Details = new Details();

        if (Details.ShowDialog(this) == DialogResult.OK)
        {
            OBJberenLijst.Add(Details.getdetails());
        }
        Details.Close();
        Details.Dispose();
    }

   public void LijstLaden()
    {
        foreach(Beer berenobject in OBJberenLijst)
        {
            LST_beren.Items.Add(berenobject.Naam);
        }
    }
}

}

来自此表单 details.cs

public partial class Details : Form
{
    public Details()
    {
        InitializeComponent();
        BTN_toevoegen.DialogResult = DialogResult.OK;
        BTN_cancel.DialogResult = DialogResult.Cancel;
    }

    private void Details_Load(object sender, EventArgs e)
    {
        RDB_pluche.Checked = true;
        BTN_ok.Enabled = false;
    }

    private void RDB_pluche_CheckedChanged(object sender, EventArgs e)
    {
        PANEL_pluche.Visible = true;
        PANEL_elektro.Visible = false;
    }

    private void RDB_elektro_CheckedChanged(object sender, EventArgs e)
    {
        PANEL_pluche.Visible = false;
        PANEL_elektro.Visible = true;
    }

    private void BTN_toevoegen_Click(object sender, EventArgs e)
    {
        open_foto.Filter = "jpg (*.jpg)|*.jpg|bmp(*.bmp)|*.bmp|png(*.png)|*.png";
        if (open_foto.ShowDialog() == System.Windows.Forms.DialogResult.OK && open_foto.FileName.Length > 0)
        {
            TXT_adres.Text = open_foto.FileName;
            PIC_beer.Image = Image.FromFile(open_foto.FileName);
        }

    }

    private void BTN_ok_Click(object sender, EventArgs e)
    {


    }
    public void getdetails()
    {
        if (RDB_pluche.Enabled == true)
        {
            Pluche_Beer objectBeer_pluche = new Pluche_Beer(TXTNaam_pluche.Text, open_foto.FileName, "(Wasprogramma: " + TXT_wasprogramma.ToString() + " Graden Celsius");

        }
        else
        {
            Elektronische_Beer objectBeer_Elektro = new Elektronische_Beer(TXTNaam_elekro.Text, open_foto.FileName, "aantal Batterijen: " + CMBOBatterijen.ToString());

        }

    }


    private void Details_MouseMove(object sender, MouseEventArgs e)
    {

        foreach (Control c in this.Controls)
        {
            if (c is TextBox)
            {
                TextBox textBox = c as TextBox;
                if (textBox.Text != string.Empty)
                {
                    BTN_ok.Enabled = true;
                }
            }
        }
    }
  }
}

1 个答案:

答案 0 :(得分:1)

问题出在这一行......

  

OBJberenLijst.Add(Details.getdetails());

......和这一行。

  

public void getdetails()

List.Add()需要添加一个对象,但getdetails()返回void。您可能希望将getdetails()更改为以下内容:

public Berenlijst getdetails()
{
    if (RDB_pluche.Enabled == true)
    {
        return new Pluche_Beer(TXTNaam_pluche.Text, open_foto.FileName, "(Wasprogramma: " + TXT_wasprogramma.ToString() + " Graden Celsius");

    }
        return new Elektronische_Beer(TXTNaam_elekro.Text, open_foto.FileName, "aantal Batterijen: " + CMBOBatterijen.ToString());

}

希望Berenlijst固有的Pluche_Beer和Elektronisch_Beer。否则,你将不得不以更广泛的方式修改你的逻辑。