不能从void转换为list

本文关键字:list 转换 void 不能 | 更新日期: 2023-09-27 18:15:56

所以,当我尝试编写一些新的程序时,我有点卡住了。

我正试图将objectBeer_plucheobjectBeer_Elektro添加到我的OBJberenlijst上的Beren主表单上的详细信息表单,所以我可以将2个类的两个实例添加到同一列表。

顺便说一下,我甚至不确定这是否可能。所以,我想反馈,如果我正在努力做的是可能的开始。我已经认为VOID是不对的,但我在这里真的毫无头绪。

这是我的主要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);
        }
    }
}

}

来自这个叫做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;
                }
            }
        }
    }
  }
}

不能从void转换为list

问题在这一行之间…

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());
}

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

相关文章: