Deborah's Developer MindScape






         Tips and Techniques for Web and .NET developers.

December 1, 2010

Using ConvertAll to Convert a Set of Values

Filed under: C#,Lambda Expressions,VB.NET @ 12:03 am

ConvertAll is one of those methods that is not used very often, but when you need it, it is very useful. It converts all of the elements of one list or array into element of another type.

In this example the user enters a list of numbers into a TextBox as a string, that list is converted to a list of integers, and the integers are averaged.

In C#:

string numbers = textBox1.Text;
if (!String.IsNullOrEmpty(numbers))
{
    var stringArray = numbers.Split(‘,’);
    int value;
    var numberArray = Array.ConvertAll(stringArray,
        s => int.TryParse(s, out value) ? value : 0);
    MessageBox.Show("Average is: " + numberArray.Average());
}

In VB:

Dim numbers As String = TextBox1.Text
If Not String.IsNullOrEmpty(numbers) Then
    Dim stringArray = numbers.Split(","c)
    Dim value As Integer
    Dim numberArray = Array.ConvertAll(stringArray, 
               Function(s) If(Integer.TryParse(s, value), value, 0))
    MessageBox.Show("Average is: " & numberArray.Average())
End If

This code resides in a Button Click event for a Windows form that contains a TextBox and a Button. The user enters a set of numbers into the TextBox, separated by commas, and clicks the button.

The code uses the Split function to split the entered string of numbers into an array using the comma as the separator.

The Array.ConvertAll method converts the resulting string array into an integer array using the Integer.TryParse method. If the value can be parsed to an integer, it uses the value. If not, it uses a zero. This protects the code from invalid user input.

Finally, it uses the Average method to calculate the numeric average of the resulting list.

Use this technique any time you have a list or array of values and need to convert them all to another type.

Enjoy!

5 Comments

  1.   Nick — December 1, 2010 @ 1:59 pm    Reply

    I never knew about this – good stuff….

    …but I prefer linq

    var integers = listOfNumbersAsString.Select(Convert.ToInt32);

  2.   Nick — December 1, 2010 @ 2:01 pm    Reply

    not bad, I’ve never used this.

    Cant beat LINQ though:

    var integers = stringOfNumbers.Select(Convert.ToInt32);

  3.   Jason Kendall — December 2, 2010 @ 8:02 am    Reply

    I also think that you missed a great opportunity to mention LINQ. I prefer the ‘Cast’ method.

    stringArray.Cast(Of Integer)

  4.   Rostov — December 3, 2010 @ 10:51 am    Reply

    A good article Deborah, however there are some good comments listed below.

    For what it’s worth, I think some of the suggestions of using LINQ to perform such an operation are a bit more apt (though I haven’t tested any performance between the two).

    One thing to note is that Array.ConvertAll exists back in .NET 2 — though you’re not going to be able to use a lambda as you have in your example.

    One thing the folks in the comments didn’t do that you did, was use TRYParse — in each of the other comments if whatever is in the list/array of strings couldn’t be parsed into an integer they’d throw an exception.

  5.   rrjp — July 25, 2012 @ 9:05 am    Reply

    Good article. I was also reading this:

    http://www.dotnetperls.com/array-convertall

    which says that using ConvertAll involves delegates which is less performant that writing it yourself the old fashioned way. Thought it was worth mentioning for code where performance is paramount.

RSS feed for comments on this post. TrackBack URI

Leave a comment

*

© 2014 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs