edgarmtze
edgarmtze

Reputation: 25038

How to apply += operator inside a conditional ternary operator

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

Answers (2)

Sathish
Sathish

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

dotNET
dotNET

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

Related Questions