Another post on the MSDN Forums, another idea for a blog. The title of the post was “Multiplying the Contents of an Array”. The OP wanted to know if there was a VB.NET equivalent to an Excel formula such as “=product(A1:A30)” , which would multiply all the elements in an array together.
A few Forum members responded with some usual and not so usual solutions: from a basic loop to an extension method. While .NET has some obvious built-in methods such as Sum and Average, these do not include one for the Product of all the elements of a collection. At least not an obvious one!
The Form for this is simple: just need 1 Button on the Form. And since the OP asked about an array, we’ll use that here, although you could use a List(Of T) also, which is what I would normally use.
To begin with, let’s instantiate a class-level array and populate it with a few values.
<code>
Dim myArray() As Double = New Double() {2.3, 4.8, 7.2}
</code>
Double-click the Button on the Form to create the Button Click event in the code editor, and then put in this code.
<code>
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ad As Double = MultiplyArray(myArray)
Debug.WriteLine(ad)
End Sub
</code>
Pretty straightforward so far. We create a Double variable, and set it’s value to a method named MultiplyArray, passing our Double array to the function.
Now we’ll begin building our function. As can be seen, the name is MultiplyArray, with an array of Double as the argument, returning a Double.
<code>
Private Function MultiplyArray(ByVal somenumbers As Double()) As Double
End Function
</code>
So what do we need as the working code in the Function? Since we are returning a Double, we need a Double variable. We’ll also want to test if the array is Nothing and if the array has no elements, otherwise we’ll throw an exception. If it passes both those conditions, we can use a basic loop to multiply each element.
<code>
Dim prod As Double
If Not somenumbers Is Nothing AndAlso Not somenumbers.Count = 0 Then
prod = 1
For Each d As Double In somenumbers
prod *= d
Next
End If
Return prod
</code>
The double variable defaults to 0. If the If…Then fails on either the array being Nothing or the array not having any elements, then the functions returns 0. However, if the If…Then passes, we want to change our variable. If left at 0, remember multiplying by 0 equals 0, so we’ll give it a value of 1. Now we use a loop to iterate through all the items in the array and multiply each by the value of the variable. No matter what happens to the If…Then, the function returns the Double, whether it be 0 or some other number.
The member who posted the Extension Method made a comment about not being able to shorten the code. Just what I like – a challenge. My first thought was to look through the .NET methods, so I typed myArray, followed by a period so Intellisense would show me the available options. Hmmm…nothing jumped out. So, I went back through the list and looked at the tooltips that pop up explaining each one. Again, nothing obvious. But one that looked, well, different was Aggregate. Admittedly, I have never used this method before. But I learn a lot by trying different things. I decided to give that one a try.
The first thing I noticed was that it required … you guessed it, a Lambda Expression. Imagine that, after my previous few posts. Not sure what would work, I started with a Lambda with 2 arguments, and then multiplying the 2 arguments by each other in the “body” of the Lambda.
<code>
Private Function MultiplyArray(ByVal somenumbers As Double()) As Double
Dim prod As Double
If Not somenumbers Is Nothing AndAlso Not somenumbers.Length = 0 Then
prod = somenumbers.Aggregate(Function(a, b) a * b)
End If
Return prod
End Function
</code>
It would be interesting to see what the function returned. Before I ran the app, I used a calculator to multiply the 3 numbers together. The answer was 79.488. Now let’s see what answer my app gives. I ran it, and printed in the Immedaite Window was (drum roll please):
79.488
Wow! I was surprised, to put it mildly. And on my first try- that doesn’t happen very often.
I hope this gives you not only a nice way to multiply all the elements of a collection together, but also the idea to try different things in .NET. You never know what you’ll discover and learn.








