7

Given an array of strings, say: Dim array1 As String() = {"1", "2", "3"} what is the best way to copy that array and perform an action on each element?

In other words, what is the best way to copy that array to come up with: array2 as integer() = {1, 2, 3}

For example, something similar to JavaScript's .Map function:

var numbers = [4, 9, 16, 25];

function myFunction() {
    x = document.getElementById("demo")
    x.innerHTML = numbers.map(Math.sqrt);
}
// Result: 2, 3, 4, 5

If it isn't possible in one line - as I suspect it isn't - what is your quickest alternative? Thanks!

Rhurac
  • 439
  • 4
  • 16

3 Answers3

11

If you don't want to use any LINQ extension methods, but you are okay with using lambda expressions, you can still do it in one line using Array.ConvertAll:

Dim input() As String = {"1", "2", "3"}
Dim output() As Integer = Array.ConvertAll(input, Function(x) Integer.Parse(x))

However, it does beg the question: why not just use LINQ, at that point, since it's effectively the same thing:

Dim input() As String = {"1", "2", "3"}
Dim output() As Integer = input.Select(Function(x) Integer.Parse(x)).ToArray()

The classic imperative way to do this in VB, without using LINQ or lambdas, would be a for-loop:

Dim input() As String = {"1", "2", "3"}
Dim output(LBound(input) To UBound(input)) As Integer
For i As Integer = LBound(input) To UBound(input)
    output(i) = Integer.Parse(input(i))
Next
Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
  • Thanks for the quick response, I like your answer. I chose no Linq simply because I haven't yet exposed myself to it yet and wanted to see the answers people could come up with that were not Linq. Thanks for adding the Linq query as well though, that is useful. I will edit my question to include Linq queries – Rhurac Dec 21 '16 at 15:49
5

I would like to add that, similar to JavaScript, .NET's map equivalent Select also supports method groups as well as lambdas.

Here's an example using a lambda:

Dim output = input.Select(Function(x) SomeMethod(x)).ToArray()

Here's an example using a method group. Since parenthesis on method invocations are optional in VB.NET, the additional AddressOf keyword is required:

Dim output = input.Select(AddressOf SomeMethod).ToArray()

For completeness, here's an example using the LINQ query syntax, which is just syntactic sugar for the first example:

Dim output = (From x In input Select SomeMethod(x)).ToArray()
Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • Thanks this is very useful information I was not aware of – Rhurac Dec 21 '16 at 15:55
  • @Rhurac, the same is true of `Array.Convert`. Any method that has a delegate for a parameter can be given either a delegate object (e.g. `AddressOf SomeMethod`) or an inline lambda expression as the argument – Steven Doggart Dec 21 '16 at 15:58
1

If you don't want to use LINQ here is the classic way, a loop:

Dim numbers = {4, 9, 16, 25}
For i As Int32 = 0 To numbers.Length - 1
    numbers(i) = CInt(Math.Sqrt(numbers(i)))
Next
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939