简体   繁体   English

c# - 无法从 void 转换为列表

[英]c# - Cannot convert from void to list

So, I got kind of stuck over my head while I tried to program something new.所以,当我尝试编写一些新的东西时,我有点不知所措。

I'm trying to add objectBeer_pluche or objectBeer_Elektro to my OBJberenlijst on the Beren Main form from the details Form, so I can add both instances of 2 classes to the same list.我正在尝试将objectBeer_plucheobjectBeer_Elektro添加到我的OBJberenlijstBeren Main 表单的详细信息表单中,因此我可以将 2 个类的两个实例添加到同一个列表中。

I'm not even sure this is possible by the way.顺便说一下,我什至不确定这是可能的。 So, I would like feedback if what I am trying to do is possible to start with.所以,如果我正在尝试做的事情可以开始,我希望得到反馈。 I already figured VOID is not right but I am really clueless here.我已经认为 VOID 是不对的,但我在这里真的一无所知。

This is my main beren.cs form with an OBJberenlist , that's where I try to add objectBeer_pluche or objectBeer_Elektro into it:这是我的主要beren.cs表单,带有OBJberenlist ,这就是我尝试将objectBeer_plucheobjectBeer_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);
        }
    }
}

} }

from this form called details.cs来自这个名为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;
                }
            }
        }
    }
  }
}

The problem is between this line...问题是在这条线之间......

OBJberenLijst.Add(Details.getdetails()); OBJberenLijst.Add(Details.getdetails());

...and this line. ...还有这条线。

public void getdetails() public void getdetails()

List.Add() requires an object to add, but getdetails() returns void. List.Add() 需要添加一个对象,但 getdetails() 返回 void。 You probably want to change getdetails() to something like the following:您可能希望将 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());

}

Hopefully Pluche_Beer and Elektronisch_Beer inherent from Berenlijst.希望 Pluche_Beer 和 Elektronisch_Beer 来自 Berenlijst。 Otherwise you'll have to revise your logic in a broader way.否则,您将不得不以更广泛的方式修改您的逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM