views:

106

answers:

2

Is there a built-in way to multiply every member of an array by some number in-place?

Example:

Dim volts () as Double = {1.243, 0.534, 5.343, 2.223, 4.334}
Dim millivolts (4) as Double = volts.MultiplyEachBy(1000) 'something like this
+7  A: 

You can use the Array.ConvertAll method.

Array.ConvertAll(volts, Function(x) x * 1000)

EDIT

There is a small error in the sample code which needs to be corrected for the above to compile. Remove the explicit size (4) from the variable type

Dim volts() As Double = {1.243, 0.534, 5.343, 2.223, 4.334}
JaredPar
@Dario, yes corrected
JaredPar
@Steven, there is a slight error in how you are declaring the variable volts. You need to remove the explicit size 4
JaredPar
A: 

I don't think there is a built in way to do this, but the best thing I could think to do would be to just create your own method. Something like

Public Function convertMilliamps(ByVal voltArray() As Double)
    For Each item AS Double In voltArray
        item = item * 1000 
    Next

Return voltArray()
End Function

then just do volts = convertMilliamps(volts)

msarchet