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.
Code is fine. Most definitely a bug in the compiler you are using.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With