Reputation: 25038
Having a list View in Vb, and having a variable total8KI how can I accumulate its value but checking if it is not null or empty at the same time?
I would like to accomplish something like
Dim total8KI As Double
For Each itm As ListViewItem In lv.Items
total8KI += CDbl(itm.SubItems(27).Text)
Next
But adding condition if is null or empty assign 0 else assign the value so
Dim total8KI As Double
For Each itm As ListViewItem In lv.Items
IIf(total8KI = String.IsNullOrEmpty(CDbl(itm.SubItems(27).Text)), CDbl(itm.SubItems(27).Text), 0)
Next
The problem is that I can not apply +=
inside ternaru operator
Is there a way to acomplish this in one line or Do I have to use
If (String.IsNullOrEmpty(CDbl(itm.SubItems(27).Text))) Then
total8KI += CDbl(itm.SubItems(27).Text)
Else
total8KI += 0
End If
Upvotes: 2
Views: 71
Reputation: 4487
Change this
IIf(total8KI = String.IsNullOrEmpty(CDbl(itm.SubItems(27).Text)), CDbl(itm.SubItems(27).Text), 0)
To
total8KI+=If(String.IsNullOrEmpty(CDbl(itm.SubItems(27).Text)), CDbl(itm.SubItems(27).Text), 0)
Upvotes: 2
Reputation: 35400
Use the new IF
(not supported in old versions of VB.NET):
total8KI += If(String.IsNullOrEmpty(itm.SubItems(27).Text), CDbl(itm.SubItems(27).Text), 0)
Or safer:
total8KI += If(IsNumeric(itm.SubItems(27).Text), CDbl(itm.SubItems(27).Text), 0)
if you expect non-empty, non-numeric values could be there.
Upvotes: 4