Reputation: 1
pls copy paste program below and try it out... '************ Imports System
Module Program Sub inputproc(ByRef pstudarr() As String) Dim count As Integer For count = 0 To 3 Console.WriteLine("Input name " & count) pstudarr(count) = Console.ReadLine Next End Sub
Sub display(ByRef pstudarr() As String)
Dim count As Integer
For count = 0 To 3
Console.WriteLine(pstudarr(count))
pstudarr(count) = "zzzzzzz"
Next
End Sub
Sub Main(args As String())
Dim studarr(3) As String
Call inputproc(studarr)
Call display(studarr)
Console.ReadLine()
Call display(studarr)
End Sub
End Module '*************
next replace Sub display(ByRef pstudarr() As String)
with Sub display(Byval pstudarr() As String)
You will see... it is same output
SO IT SEEMS NO DIFFERENCE WITH BYVAL AND BYREF IN ARRAYS???
same output... seems byval and byref has no difference for arrays.
Anyone having same issues???
Upvotes: -2
Views: 96
Reputation: 253
Types in .Net come in two basic flavors, they can be either a value type or a reference type.
Booleans, Integers, Shorts, Singles, Doubles are value types. All structures are value types too. If a value type is passed by value, then changing it's value from within a function will not affect the value outside of the function.
Classes in .Net are reference types. It doesn't matter if you passed them by value or by reference, changes made within the function to that class-based object will be reflected outside of the function. The Array type is a class.
Naturally one might wonder what would be the point of allowing reference types to be passed by value or by reference since it doesn't seem to matter but there is a scenario where it does make a difference. When a reference type is passed by reference, the reference itself can be changed from within the function. For example:-
Sub Main()
Dim outsideArr As Integer() = {10, 20}
TryChange(outsideArr)
Debug.WriteLine(String.Join(", ", outsideArr))
End Sub
Private Sub TryChange(ByVal arrParam As Integer())
'Change the values in the array
arrParam(0) = 100
arrParam(1) = 200
'Assign a completely new array
arrParam = {10000, 2000}
End Sub
The above code demonstrates this. When the array is passed by value, you will see the changes to the array itself being reflected outside despite the fact that the last thing we did is to assign a completely different array to the sub's parameter. We can change the values inside of array because arrays are reference types but we cannot change the actual reference.
Try changing ByVal to ByRef and you will see different behavior. Now you will see the values of the array we assigned before leaving the sub being printed because we can now change the reference itself from within the sub thanks to ByRef.
Upvotes: 0
Reputation: 82534
That's because an array is a reference type.
Passing a reference type to a method using ByVal
means that the reference is passed by value - so if you change the reference to point to another instance of array, the changes will not reflect outside of your method.
If you are passing the reference ByRef
, and assign it to another instance inside the method, that would reflect outside as well.
For more details, read Jon Skeet's Parameter passing in C# (Don't worry, this also applies to Vb.Net).
Upvotes: 1