Mog*_*gli 0 c# conditional-operator winforms
我想在这里做一件非常简单的事 -
我需要在表格布局面板中找到一个控件
1)如果控件存在 - 删除控件
2)否则我不需要做任何事情
除了if\else之外,我决定使用条件运算符.我的代码是:
var temp=(tableLayoutExamPanel.Controls.Find("lbl3", true)[0].Name==("lbl3")) ? (tableLayoutExamPanel.Controls.Find("lbl3", true)[0].Dispose()) : null ;
Run Code Online (Sandbox Code Playgroud)
但我面临这个错误:
Type of conditional expression cannot be determined because there is no implicit conversion between 'void' and '<null>'
Run Code Online (Sandbox Code Playgroud)
谷歌有这么多的解决方案,但没有一个适合我,我没有找到任何无效的解决方案,陷入其中,需要帮助.
提前致谢.
Dispose不返回任何东西,它的返回值为void.void无法分配给变量.
在您的场景中使用条件运算符根本没有任何意义.只需使用:
var control = tableLayoutExamPanel.Controls.Find("lbl3", true)[0];
if(control.Name==("lbl3"))
control.Dispose();
Run Code Online (Sandbox Code Playgroud)