none
Passing array by value RRS feed

  • Question

  • Hi,

    Is there any way to pass an array to a method by value?

    I can walk around this by creating a temporary array to be passed to the method; but doing this too much is very annoying.

    Thanks to any help.  

    Friday, April 26, 2013 10:54 AM

Answers

  • Does this help as start ? 

    
    Module Module1
        Sub Main()
            Dim arr() As String = {"Freddy", "Brian", "Mary", "Jane"}
            Console.WriteLine("Before Method1 Call")
            Console.WriteLine(arr(0))
            Method1(DirectCast(arr.Clone(), String()))
            Console.WriteLine("After Method1 Call")
            Console.WriteLine(arr(0))
        End Sub
        Private Sub Method1(ByVal a() As String)
            a(0) = "Smithers"   
        End Sub
    End Module
    

    • Proposed as answer by .paul. _ Saturday, April 27, 2013 1:43 AM
    • Marked as answer by BGQQ Saturday, April 27, 2013 7:00 PM
    Friday, April 26, 2013 8:55 PM

All replies

  • I am not aware of any way to do this. Others can correct me if this is not the case, but .Net arrays are object types. When you pass an array, you are really passing around a pointer to the object. There is no difference between passing an Array ByVal or ByRef.

    If you want to pass the array, make changes to it in your method, then when you return to your main sub have the array remain untouched, unfortunately there is no other way to do it. You can make it simpler by writing methods to do that for you though (this example uses generic lists, but can be retooled for any other array type):

        Private Sub DoWork(ByVal MyArray As List(Of String))
    
            'Do work here
    
        End Sub
    
        Private Function CopyListofString(ByVal SourceArray As List(Of String)) As List(Of String)
    
            Dim NewList As New List(Of String)
            NewList.AddRange(SourceArray)
            Return NewList
    
        End Function

    To call your copy function:

            'Your array variable
            Dim MyStringList As New List(Of String)
    
            'Call your function
            DoWork(CopyListofString(MyStringList))
    By using the CopyListofString function, you pass a new copy of the array so the original remains untouched.

    Friday, April 26, 2013 11:46 AM
  • Hi

    Is it as simple as this? Or maybe I am missing something.

    Public Class Form1
        Dim arr() As String = {"Freddy", "Brian", "Mary", "Jane"}
        Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
            Method1(arr)
        End Sub
        Private Sub Method1(ByVal a() As String)
            a(0) = "Smithers"
        End Sub
    End Class
    


    Regards Les, Livingston, Scotland

    Friday, April 26, 2013 11:46 AM
  • It sounds strange, can you more explain what "you" mean with byval, probably we have other ideas around that. 

    Be aware a method is itself also only a reference point (a created stack object) not a fixed place in memory.


    Success
    Cor

    Friday, April 26, 2013 12:36 PM
  • Hi

    Is it as simple as this? Or maybe I am missing something.

    Public Class Form1
        Dim arr() As String = {"Freddy", "Brian", "Mary", "Jane"}
        Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
            Method1(arr)
        End Sub
        Private Sub Method1(ByVal a() As String)
            a(0) = "Smithers"
        End Sub
    End Class


    Regards Les, Livingston, Scotland

    It does not work; arr(0) has really modified and become "Smithers".
    Friday, April 26, 2013 8:44 PM
  • Does this help as start ? 

    
    Module Module1
        Sub Main()
            Dim arr() As String = {"Freddy", "Brian", "Mary", "Jane"}
            Console.WriteLine("Before Method1 Call")
            Console.WriteLine(arr(0))
            Method1(DirectCast(arr.Clone(), String()))
            Console.WriteLine("After Method1 Call")
            Console.WriteLine(arr(0))
        End Sub
        Private Sub Method1(ByVal a() As String)
            a(0) = "Smithers"   
        End Sub
    End Module
    

    • Proposed as answer by .paul. _ Saturday, April 27, 2013 1:43 AM
    • Marked as answer by BGQQ Saturday, April 27, 2013 7:00 PM
    Friday, April 26, 2013 8:55 PM
  • Does this help as start ? 

    Module Module1
        Sub Main()
            Dim arr() As String = {"Freddy", "Brian", "Mary", "Jane"}
            Console.WriteLine("Before Method1 Call")
            Console.WriteLine(arr(0))
            Method1(DirectCast(arr.Clone(), String()))
            Console.WriteLine("After Method1 Call")
            Console.WriteLine(arr(0))
        End Sub
        Private Sub Method1(ByVal a() As String)
            a(0) = "Smithers"   
        End Sub
    End Module
    

    It definitely helps; thank you very much.
    Saturday, April 27, 2013 7:01 PM