EN
|
NL
dec 30

Geplaatst door: André Obelink (Host)
30-12-2008 23:41 

Visual Basic 2008 in combination with the .NET framework 3.5 brings us a lot of new powerful features. Some of them are obvious like LINQ or XML literals; other new features are less obvious. Some very handy features are the new (extension) methods on arrays. Methods for concatenating of arrays, filtering or even mathematical operations like the minimum- or maximum value or the sum of all elements. Many of these new features are the directly related - or better said - are introduced as a pretty side effect of LINQ.

To merge two arrays you can use the Concat() method. In this example we define two arrays with numbers. The variable numbersBoth() will contain the result of both arrays. Call the Concat() method and pass the other array as argument.

   1: ' Define two arrays
   2: Dim numbersOdd() As Integer = {1, 3, 5, 7, 9}
   3: Dim numbersEven() As Integer = {2, 4, 6, 8, 10}
   4: 
   5: ' Concatenate both arrays
   6: Dim numbersBoth() As Integer = numbersOdd.Concat(numbersEven).ToArray
   7: 
   8: ' Show all numbers in the concatenated array
   9: For Each number As Integer In numbersBoth
  10:     Console.Write(number & " - ")
  11: Next
  12: 
  13: 'Result: 1 - 3 - 5 - 7 - 9 - 2 - 4 - 6 - 8 - 10 - 


If you ever wrote some database code and build your SQL queries, perhaps you have used the DISTINCT operator. This keyword will filter your data, so the result contains only unique records or rows. With the new Distinct() method you can do same trick on the content of your array. For sake of performance I can recommend to sort the array first. In this case all the elements are already sorted, so this is not necessary.

   1: ' Define an array
   2: Dim numbers() As Integer = {1, 2, 2, 3, 4, 4, 5, 5, 6, 7, 8, 8, 9, 10}
   3: 
   4: ' Use the Distinct() method to get unique results
   5: For Each number In numbers.Distinct.ToArray()
   6:     Console.Write(number & " - ")
   7: Next
   8: 
   9: ' Result: 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10 -


Do you want to know which content of two arrays are in common? Use the Intersect() method. It will return an array with all elements which exists in both arrays. In the example below the only element which exists in both arrays is 'Amsterdam', but you can imagine that this could be any number of elements.

   1: ' Define two arrays
   2: Dim citiesHolland() As String = {"Amsterdam", "Rotterdam", "Utrecht"}
   3: Dim citiesEurope() As String = {"London", "Paris", "Amsterdam", "Madrid"}
   4: 
   5: ' Get the elements which are common in both arrays
   6: Dim citiesCommon() As String = citiesEurope.Intersect(citiesHolland).ToArray()
   7: 
   8: ' Show 'all' elements in the result
   9: For Each city As String In citiesCommon
  10:      Console.Write(city & " - ")
  11: Next
  12: 
  13: ' Result: Amsterdam -


If you want to know the opposite of the result of the Intersect() method, call the Except() method. The result of this method will only contain these elements which are not exists in the other array.

   1: ' Define two arrays
   2: Dim citiesHolland() As String = {"Amsterdam", "Rotterdam", "Utrecht"}
   3: Dim citiesEurope() As String = {"London", "Paris", "Amsterdam", "Madrid"}
   4: 
   5: ' Get the elements which are unique in both arrays
   6: Dim citiesUnique() As String = citiesEurope.Except(citiesHolland).ToArray
   7: 
   8: ' Show all elements in the result
   9: For Each city As String In citiesUnique
  10:     Console.Write(city & " - ")
  11: Next
  12: 
  13: ' Result: London - Paris - Madrid -


For purposes of calculation arrays contains now a couple of methods for various mathematical operations, like Min(), Max(), Average() or Sum(). I think the code below doesn't need some extra explanation.

   1: ' Define an array
   2: Dim numbers() As Integer = {23, 2, 9, 1, 34, 12}
   3: 
   4: Console.WriteLine("Minimum value: " & numbers.Min())
   5: Console.WriteLine("Maximum value: " & numbers.Max())
   6: Console.WriteLine("Average of all numbers: " & numbers.Average())
   7: Console.WriteLine("Sum of all numbers: " & numbers.Sum())
   8: 
   9: ' Result:
  10: ' Minimum value: 1
  11: ' Maximum value: 34
  12: ' Average of all numbers: 13,5
  13: ' Sum of all numbers: 81


The methods mentioned in this article are only an excerpt of all the new stuff. As you can see a couple of them are real powerful and very easy to use. I can recommend to take a closer look to all of them. You can recognize these new methods by a different icon in the IntelliSense window (See the blue arrow). 

image

Happy coding!

Tags:

2 commentaar(en) tot op heden...

Re: VB2008 - Extension methods of arrays

What is the best platform to test these codes?

Door: INNRECH op:   10-3-2009 19:58

Re: VB2008 - Extension methods of arrays

The codes you provided above are actually helpful and it gives a direct access to the use of many key words.

Door: INNRECH op:   3-3-2009 1:27

Uw naam:
Uw email:
(Optioneel) Email wordt alleen gebruikt om een Gravatar te tonen.
Uw website:
Onderwerp:
Commentaar:
Security Code
Neem de code over
Voeg commentaar toe   Annuleren 
Blog
Blog archive
 

Willem Dreeslaan 63, 7103 JE  Winterswijk |  Postbus 428, 7100 AK Winterswijk  |  M 06-47914464  |  E info@maryor.nl