我需要检查是否有大量的控件包含值或者它们是否留空。C# - 语句在switch语句中
我希望做这样的事情:
public static bool IsObjectEmpty(Control ctrlThis)
{
switch (ctrlThis)
{
case ctrlThis is TextBox:
TextBox txtThis = (TextBox)ctrlThis;
if (txtThis.Text == "" || txtThis.Text == null)
{ return false; }
break;
case (ctrlThis is ComboBox):
ComboBox cboThis = (ComboBox)ctrlThis;
if (cboThis.SelectedValue == -1)
{ return false; }
break;
case (ctrlThis is NumericUpDown):
NumericUpDown numThis = (NumericUpDown)ctrlThis;
if (numThis.Value == 0)
{ return false; }
break;
etc etc...
但是,这并不编译:
Error 3 A switch expression or case label must be a bool, char, string,
integral, enum, or corresponding nullable type
有switch语句这样做的方式,还是我只是如果有东西需要加载if/else? Google和StackOverflow搜索没有任何用处。
非常量..文字.. – Anirudha
这是错误的原因,但不是真正的问题的答案。 – Sjoerd
@ Some1.Kill.The.DJ我让我的回答更清楚。 –