Array.Average usage?

  • Thread starter Thread starter Christian Reizlein
  • Start date Start date
C

Christian Reizlein

I have an array with a bounch of values and i would like to return the sum
of them all divided by the amount, basically, the average, i saw there is a
average function in the array class but i cannot get it working, does anyone
have an example?

Thanks
 
Christian Reizlein said:
I have an array with a bounch of values and i would like to return
the sum of them all divided by the amount, basically, the average, i
saw there is a average function in the array class but i cannot get
it working, does anyone have an example?

dim av as double

av = YourArray.average


Armin
 
Well, looks pretty simply that way, my problem is that myArray is an
arraylist
so i was using this

dim ar as new arraylist
dim v as double
v = ar.toarray.average
and that fails since it need more params.

any ideas?
 
Christian Reizlein said:
Well, looks pretty simply that way, my problem is that myArray is an
arraylist
so i was using this

dim ar as new arraylist
dim v as double
v = ar.toarray.average
and that fails since it need more params.

any ideas?

Average is overloaded for several types of items in an enumerable
object. See System.Linq.Enumerable.Average in the object browser. For
example, if you have Integer values in the arraylist, cast the result to
an Integer-array:

v = directcast(ar.toarray, integer()).average

However, I suggest you make use of a List(Of Integer) instead of an
Arraylist. This also enables a direct call to YourList.Average without
creating an array first.


Armin
 
Amazing, thanks! i didnt even know the existant of that object
I certainly used it now and the code looks great.

Thanks,
-CR
 
Amazing, thanks! i didnt even know the existant of that object
I certainly used it now and the code looks great.
Class instead of object, you are creating objects.

Cor
 
Back
Top