2013-01-04 158 views
3

我需要检查是否有大量的控件包含值或者它们是否留空。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搜索没有任何用处。

回答

2

Case标签只能包含常量表达式。

所以在你的答案是不是一个const表达式。

这是一个评估值。

一样多,你不能这样做

public const int a=Enumerable.Range(0,2).First();

可以开关的情况下

前calc下的值,然后比较它们的值。

var t=(ctrlThis is ComboBox)

... 
... 

switch (t) ... 

case true :... 

编辑:从CLS

switch-label: 
    case constant-expression : 
    default : 

如果你不那样做,编译器会尖叫:

常量预计值为

例如:

switch (myInt) 
{ 
case (2+Enumerable.Range(0,2).First()): 
    return true; 
    default: 
    return true; 
} 
+1

非常量..文字.. – Anirudha

+3

这是错误的原因,但不是真正的问题的答案。 – Sjoerd

+0

@ Some1.Kill.The.DJ我让我的回答更清楚。 –

1

你可以这样做:

switch (ctrlThis.GetType().ToString()) 
{ 
    case "System.Windows.Forms.TextBox" : 
      TextBox txtThis = (TextBox)ctrlThis; 
       if (txtThis.Text == "" || txtThis.Text == null) 
       { return false; } 
       break; 
} 
+0

然而,如果这个类型被重命名,这将是不安全的 - 你的解决方案仍然会编译,但永远不会到达处理程序。一个典型的''''''查询不会再编译,因为这个类型不再被知道。 –

+0

@AlexanderWeinert你是对的。但是,如果交换机只检查系统类型,并且您想要一个精确的类型匹配,那就相当安全。 – Magnus

2

条件(如果/开关)基于类型大多是一个坏主意。

如何对这种做法:

public static bool IsObjectEmpty(TextBox ctrlThis) 
{ 
    if (ctrlThis.Text == "" || ctrlThis.Text == null) { 
     return false; 
    } 

    etc etc... 
}    

public static bool IsObjectEmpty(ComboBox ctrlThis) 
{ 
    if (ctrlThis.SelectedValue == -1) { 
     return false; 
    } 

    etc etc... 
}    

public static bool IsObjectEmpty(NumericUpDown ctrlThis) 
{ 
    if (ctrlThis.Value == 0) { 
     return false; 
    } 

    etc etc... 
} 
0

据我知道你能做到这样

public static bool IsObjectEmpty(Control ctrlThis) 
{ 
    Type t = ctrlThis.GetType(); 
    switch (t) 
    { 
     case typeof(TextBox): 
      TextBox txtThis = (TextBox)ctrlThis; 
      if (txtThis.Text == "" || txtThis.Text == null) 
      { return false; } 
      break; 
    } 
} 
0

我可能会去的if语句藏汉,如:

public static bool IsObjectEmpty(Control ctrlThis) 
    { 
     if (ctrlThis is TextBox) 
     { 
      TextBox txtThis = (TextBox)ctrlThis; 
      if (txtThis.Text == "" || txtThis.Text == null) 
       return false; 
     } 
     else if (ctrlThis is ComboBox) 
     { 
      ComboBox cboThis = (ComboBox)ctrlThis; 
      if (int.Parse(cboThis.SelectedValue.ToString()) == -1) 
       return false; 
     } 
     else if (ctrlThis is NumericUpDown) 
     { 
      NumericUpDown numThis = (NumericUpDown)ctrlThis; 
      if (numThis.Value == 0) 
       return false; 
     } 
     else 
     { 
      //Serves as 'default' in the switch 
     } 
     return true; 
    }