Passing an array element by reference

I am compiling the following code using Mono compiler v4.2.1 ( .NET Framework 4.5):

 Module Module1 Sub Change(ByRef x As Integer) x += 1 End Sub Sub Main() Dim arr() As Integer = {1} Change(arr(0)) Console.WriteLine(arr(0)) End Sub End Module 

The result: 1. Why is the parameter x not associated with the array element by reference? Is there a way to make this work without having to pass an array?

Additional information :

  • I am running the above code through HackerRank

  • The above code outputs result 2 when starting from Visual Studio 2010/2012.

  • Not sure if motivation is important for the question, but passing an array element by reference is very useful. For example, you can implement Swap , which takes two elements of an array.

+6
source share
1 answer

The code is ok. Most likely, the error in the used compiler.

0
source

All Articles