m.edmondson
m.edmondson

Reputation: 30872

Shortening this IF statement

I don't like wide code, especially when it forces me to scroll. So having written this:

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

Is there a decent way to thin it down, make it more elegant?

Upvotes: 2

Views: 241

Answers (9)

Brian Gideon
Brian Gideon

Reputation: 48949

I got it down to 3 lines and 63 columns. I have replaced the traditional If construct with the newer If operator. The code will also handle the case of Text being a null reference and will short-circuit using the OrElse operator. If you are willing to declare a couple of extension methods you could whittle this down to one short line, but that conflicts with the spirit of my answer.

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

Upvotes: 0

MarkJ
MarkJ

Reputation: 30398

We should at least mention Select Case

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

Also consider extracting a helper function

Function IsNotApplicable(ByVal s As String) As Boolean
  Return (s = "N/A") Or (s = "")
End Function

Upvotes: 1

Jürgen Steinblock
Jürgen Steinblock

Reputation: 31723

I would suppgest an Helper Class or ExtensionMethod:

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

Upvotes: 0

P&#233;ter T&#246;r&#246;k
P&#233;ter T&#246;r&#246;k

Reputation: 116266

Extract _item.SubItems(pd.perioddate).Text into a local variable, e.g.

String text = _item.SubItems(pd.perioddate).Text

If text = "N/A" Or text = String.Empty Then
            dtpDeadlineforAP.Checked = False
End If

Alternatively, you may want to extract the whole check into a separate method:

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

This would make the code more readable and allow you to reuse the checking logic.

Upvotes: 5

Franci Penov
Franci Penov

Reputation: 75981

As others suggested, you can use a local variable. You could also shorten the line additionally, by using the line continuation character _.

String period = _item.SubItems(pd.perioddate);

If period = "N/A" Or _
   period = String.Empty Then
        dtpDeadlineforAP.Checked = False
End If

Upvotes: 0

Heinzi
Heinzi

Reputation: 172220

If there are multiple fields that can contain N/A, I'd suggest the following approach:

Dim invalidValues As String() = {"N/A", String.Empty}

If invalidValues.Contains(_item.SubItems(pd.perioddate).Text) Then
    dtpDeadlineforAP.Checked = False
End If

Or, if it's just about scrolling, you can use the VB line continuation character _:

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

BTW: Here, I'd suggest OrElse instead of Or.

Upvotes: 1

Stefanvds
Stefanvds

Reputation: 5916

string obj = _item.SubItems(pd.perioddate).Text;

If obj = "N/A" Or obj = String.Empty Then
            dtpDeadlineforAP.Checked = False
End If

ALSO

enable Word Warp in visual studio to stop having to scroll.

go to

Tools->Options->Text Editor->All languages->Word Warp

dont forget to enable 'Show all settings'

Upvotes: 1

El Ronnoco
El Ronnoco

Reputation: 11922

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

Cue argument regarding the merits/evils of WITH :)

Upvotes: 1

Jagmag
Jagmag

Reputation: 10356

Dim date as String = _item.SubItems(pd.perioddate).Text

If date = "N/A" Or date = String.Empty Then 
            dtpDeadlineforAP.Checked = False 
End If

Upvotes: 0

Related Questions