Maf Kazama Maf Kazama - 2 months ago 7
Vb.net Question

How to count value in array with exception?

I have an array that having integer value,

Dim array1() As Integer = New Integer() {14,12,0,4,25,0}


I would like to count except zero number, hope the result is
4
. I was try to find the article in the internet but almost isn't exception.

Answer

I used a Lambda Expression and .FindAll:

    Dim array1() As Integer = New Integer() {14, 12, 0, 4, 25, 0}
    Dim matchedItems() As Integer = Array.FindAll(array1, _
                                        Function(x) x > 0)
    MsgBox(matchedItems.Count)

It will find all items within the array where the item is > 0.