Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 produced output is: 1. Why doesn't the x parameter get bound to the array element by reference? Is there a way to make this work without having to pass the array?

Additional details:

  • I am running the above code through HackerRank

  • The above code produces the output of 2 when run from Visual Studio 2010/2012.

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

like image 206
AlwaysLearning Avatar asked Jan 05 '16 09:01

AlwaysLearning


1 Answers

Code is fine. Most definitely a bug in the compiler you are using.

like image 192
jitendragarg Avatar answered Oct 20 '22 19:10

jitendragarg