代码之家  ›  专栏  ›  技术社区  ›  m.edmondson

缩短此IF语句

  •  2
  • m.edmondson  · 技术社区  · 14 年前

    If _item.SubItems(pd.perioddate).Text = "N/A" Or _item.SubItems(pd.perioddate).Text = String.Empty Then
                dtpDeadlineforAP.Checked = False
    End If
    

    9 回复  |  直到 14 年前
        1
  •  5
  •   Péter Török    14 年前

    提取 _item.SubItems(pd.perioddate).Text 变成局部变量,例如。

    String text = _item.SubItems(pd.perioddate).Text
    
    If text = "N/A" Or text = String.Empty Then
                dtpDeadlineforAP.Checked = False
    End If
    

    或者,您可能希望将整个支票提取到一个单独的方法中:

    If isNotFilled(_item.SubItems(pd.perioddate)) Then
                dtpDeadlineforAP.Checked = False
    End If
    

    这将使代码更具可读性,并允许您重用检查逻辑。

        2
  •  1
  •   El Ronnoco    14 年前
    With _item.SubItems(pd.perioddate)
        If .Text = "N/A" Or .Text = String.Empty Then
            dtpDeadlineforAP.Checked = False
        End If
    End With
    

    关于…的优点/缺点的暗示论点:)

        3
  •  1
  •   Stefanvds    14 年前
    string obj = _item.SubItems(pd.perioddate).Text;
    
    If obj = "N/A" Or obj = String.Empty Then
                dtpDeadlineforAP.Checked = False
    End If
    

    产科加强生命支持

    在visualstudio中启用wordwarp以停止滚动。

    工具->选项->文本编辑器->所有语言->文字扭曲

    别忘了启用“显示所有设置”

        4
  •  1
  •   Heinzi    14 年前

    Dim invalidValues As String() = {"N/A", String.Empty}
    
    If invalidValues.Contains(_item.SubItems(pd.perioddate).Text) Then
        dtpDeadlineforAP.Checked = False
    End If
    

    或者,如果只是滚动,可以使用VB行继续字符 _ :

    If _item.SubItems(pd.perioddate).Text = "N/A" _
    Or _item.SubItems(pd.perioddate).Text = String.Empty Then
        dtpDeadlineforAP.Checked = False
    End If
    

    OrElse 而不是 Or .

        5
  •  1
  •   MarkJ    14 年前

    我们至少应该提到 Select Case

    Select Case _item.SubItems(pd.perioddate).Text     
      Case "N/A", "" 
        dtpDeadlineforAP.Checked = False 
    End Select 
    

    Function IsNotApplicable(ByVal s As String) As Boolean
      Return (s = "N/A") Or (s = "")
    End Function
    
        6
  •  0
  •   Jagmag    14 年前
    Dim date as String = _item.SubItems(pd.perioddate).Text
    
    If date = "N/A" Or date = String.Empty Then 
                dtpDeadlineforAP.Checked = False 
    End If
    
        7
  •  0
  •   Franci Penov    14 年前

    正如其他人所建议的,可以使用局部变量。您还可以使用 line continuation 性格 _ .

    String period = _item.SubItems(pd.perioddate);
    
    If period = "N/A" Or _
       period = String.Empty Then
            dtpDeadlineforAP.Checked = False
    End If
    
        8
  •  0
  •   Jürgen Steinblock    14 年前

    我支持助手类或扩展方法:

    If StringIsNullOrEmptyOrNA(stringval) Then
         ...
    End If
    
    
    If stringval.IsNullOrEmptyOrNa() Then
         ....
    End If
    
    
    Public Function StringIsNullOrEmptyOrNA(ByVal input as String) as Boolean
        return String.IsNullOrEmpty(input) OrElse input.Equals("N/A")
    End Function
    
    
    <System.Runtime.CompilerServices.Extension()> 
    Public Function IsNullOrEmptyOrNa(ByVal input As String)
        return String.IsNullOrEmpty(input) OrElse input.Equals("N/A")
    End Sub
    
        9
  •  0
  •   Brian Gideon    14 年前

    我把它缩小到3行63列。我已经取代了传统的 If construct 用新的 If operator . 代码还将处理 Text OrElse 接线员。如果您愿意声明几个扩展方法,您可以将其缩减为一行,但这与我的回答的精神相冲突。

    Dim tx = _item.SubItems(pd.perioddate).Text
    Dim dtp = dtpDeadlineforAP
    dtp.Checked = If(tx = "N/A" OrElse tx = "", False, dtp.Checked)