Well, first of all, an ArrayList
is not a good choice to store values of the same type. You should rather use a List(Of Double)
, then each value doesn't have to be cast to double when you access it.
Anyhow, you can make your code a lot simpler by just setting the sum to zero before you start:
dblVal = 0.0
For i = 0 To arrLst.Count - 1
dblVal = dblVal + arrLst.Item(i)
Next
(I know that the variable is zero by default when you declare it, so you could actually skip setting it to zero, but it's good to actually set values that the code relies on.)
Using For each
and the +=
operator it gets even simpler:
dblVal = 0.0
For Each n In arrLst
dblVal += n
Next
You can also use the method Sum
to add all the values, but as you use an ArrayList
you have to make it a collection of doubles first:
dblVal = arrLst.Cast(of Double).Sum()