0

有人可以帮我解决这个问题,我不知道我应该做什么。
这是错误:

错误 1 ​​无法将类型“object”隐式转换为“Flashloader.Controller”。存在显式转换(您是否缺少演员表?)

这是我的来源:

public partial class NewApplication : Form
{

    private toepassinginifile _toepassinginifile;
    private controllerinifile _controllerinifile;



    //private controllerinifile _controlIniFile;

    public Toepassing toepassing = new Toepassing();

    public NewApplication( toepassinginifile iniFile)
    {
        _toepassinginifile = iniFile;
        _controllerinifile = new controllerinifile();

        controllerComboBox.DataSource = _controllerinifile.Controllers;

        InitializeComponent();
    }
    private void button4_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        openFileDialog1.Filter = "Srec Files (.a20; .a21; .a26; .a44)|*.a20; *.a21; *.a26; *.a44|All files (*.*)|*.*";

        openFileDialog1.Title = ("Choose a file");
        openFileDialog1.InitialDirectory = Path.Combine(Directory.GetCurrentDirectory());
        openFileDialog1.RestoreDirectory = true;
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            fileBox.Text = (System.IO.Path.GetFileName(openFileDialog1.FileName));
        }
    }

    private void button3_Click(object sender, EventArgs e)
    {
        toepassing.Name = nameBox.Text;
   ---->#toepassing.Controller = controllerComboBox.SelectedItem;#
        toepassing.TabTip = descBox.Text;
        toepassing.Lastfile = openFileDialog1.FileName;
        fileBox.Text = openFileDialog1.FileName;


        if (nameBox.Text == "")
            MessageBox.Show("You haven't assigned a Name");
        else if (controllerComboBox.Text == "")
            MessageBox.Show("You haven't assigned a Controller");
        else if (descBox.Text == "")
            MessageBox.Show("You haven't assigned a Desciption");
        else if (fileBox.Text == "")
            MessageBox.Show("You haven't assigned a Applicationfile");
        _toepassinginifile.ToePassingen.Add(toepassing);
        _toepassinginifile.Save();

        MessageBox.Show("Save Succesfull");



        this.Close();
    }
}

如何解决这个问题,找不到问题,因为我想将我的组合框连接到我的 ini 文件和我已经拥有的功能,但在某种程度上我得到了这个错误。

4

2 回答 2

2

如错误消息所述,请编写显式转换:

toepassing.Controller = (Flashloader.Controller)controllerComboBox.SelectedItem;
于 2013-06-21T08:28:02.933 回答
0

您可以尝试使用安全演员表。

就像是

toepassing.Controller = controllerComboBox.SelectedItem as Flashloader.Controller;

看看(C# 参考)

as 运算符用于在兼容的引用或可空类型之间执行某些类型的转换。

as 运算符类似于强制转换操作。但是,如果无法进行转换,则 as 返回 null 而不是引发异常。

as 运算符的好处是,您可以在之后测试变量的 null 以查看转换是否成功,而直接转换如果失败则抛出异常。

于 2013-06-21T08:29:22.807 回答