ByVal or ByRef

Hi,

I have a array of objects of a class (written by me) in my program. When i try to do some manupulation i make a function for that within the class and call the function with parameter as ByVal

soemthing like

private function NameofFunction(byval x as clsMyClass) as boolean
...
End function

Now,
When I test my program it is giving error (logically)  as it changes teh object of my array inspite of that I am just refering to it to read and no change in value.

Now Can any one please let me know. Who should i pass the variable (custom class object) so that a function doesn't change them

Thanks






Answer this question

ByVal or ByRef

  • Dave Coder

    Hi,

    You answer seems to be the solution of my problem (I haven't try it yet but will test it in few mins.) But One question that arise is that on few occasion (after getting this problem) , I try to send the values using sample code below



    function abc (byval x as myclass)
    ...
    end function
    ...
    abc(new myclass(myobject))
    ...

     

    NOw does in this way it still send the reference or val (my class again has a constructor with object parameter
    something like this


    Public Sub New(ByVal Makeas As clsPosition)

    t_x = Makeas.X

    t_y = Makeas.Y

    End Sub


     


    Does this also pass as reference or this constuctor and method of calling pass it as byval.

    Thanks



  • coreus

    Are you sure that it's not sending the clone Can you post some code that reproduces this

    Also, remember when an array is cloned, only the array is cloned not the objects contained within the array.



  • Andreas6483

    Hi,
    Sorry that I read it late as I clone my Array using temprory varibale without the Clone Method..

    can you explain a bit more on what do you mean by Array clone clones the array and not the object in it..

    I have this code



    class myclass
    ...
    end class

    class myclass2
    ...
    dim x(2,2) as myclass
    ...

    function getobject (byval tmp(,) as myclass) as boolean
    ...
    end function

    function callerone() as boolean
    return getobject(x)
    end function
    end class


     


    the code above doesn't work for me and I change the callerone as given below



    function callerone() as boolean
       dim i,j as integer
       dim tmp2(2,2) as myclass
       for i =0 to
         for j=0 to 2
           tmp2(i,j)=new myclass(x(i,j)) 'myclass has a constrcutor for this
         next
       next
       return getobject(tmp2)
    end function


     


    Well my orginail code it too big to post that is why I make this code that is giving error ..

    Can you explain if this works or not...Though I make it work with the callerone in as given below and it is working but the above one is not working

    Thanks


  • mms18

    Hi,

    Thanks for your solution, and i am still testing my code and doesn't find the reason of misbehave of it. As i am using more of your method and on only few occasion I have used Array to be passed. But both of your answer are what I required.

    Is there any specific reason that why the byval is not used when passing array and or object as david suggest.

    I mean if we are using Byval that mean it should clone it by itself and do the job.

    Thanks

  • Janee

    No. Using byval does not mean that. ByVal means that the reference will be copied, not the object itself. For simple datatypes, referense and object is the same thing. However, for complex types like class instances and arrays, reference and object are two different things. Using ByVal for complex types means that the reference of the object that will be assinged to the parameter will be different than the reference of the object used in the code that is calling the method. If the method re-assignes the parameter inside it (like methodB does in my example) the calling method will not be affected (message box still displays the same string).
    Using ByRef on the other hand means that the same reference of the object will be passed in the method. If the method re-assinges the parameter inside it (like methodC does in my example) the calling method will also be affected (message box displays the new string).

    To copy an object or arry, as David mentions requires that you copy the object entirely. You can use Array.Copy for arrays. For other classes, you will have to implement the copy code your self.


  • topmar

    David has already given the solution. I just want to mention that byref/byval makes a difference when passing object also. See the following:
    MethodB creates a new instance of the parameter but this does not affect the callers object that was passed as parameter, since the parameter is byval. MethodC however changes the callers object since the parameter if byref.



    Public Class Class1
        Public Param As String

        Sub New(ByVal mParam As String)
            Param = mParam
        End Sub
    End Class

    Public Class Class2

        Sub methodA()
            Dim obj As Class1 = New Class1("created by method A")
            MsgBox(obj.Param)      'this will prompt: created by method A
            methodB(obj)
            MsgBox(obj.Param)      'this will still prompt: created by method A
            methodC(obj)
            MsgBox(obj.Param)      'this will prompt: modified by method C
        End Sub

        Sub methodB(ByVal obj As Class1)
            obj = New Class1("modified by method B")
        End Sub

        Sub methodC(ByRef obj As Class1)
            obj = New Class1("modified by method C")
        End Sub

    End Class

     



  • Steven Habex

    Hi,

    I have try what you have suggested, But I am not getting that how I can pass a "clone" of my 2D array.

    I use this

    Myfunction(MyArray.Clone)

    But It still send it by reference and not the clone of it.

    Please let me know the syntax of how to pass 2D array as ByVal type of stuff..;)



  • sam_jeba

    Hi,

    Let me try to explain it using my original application...

    I am making a Chess game .. In this i have a Class of pieces ...
    And a GameEngine class which Has Array of Pieces.. (2D Array of 8x8) chess board...

    Now At one stage i need to Pass the Complete Array to a function to simulate teh move in chess game.. and as it is just a simulation of move i doesn't want my Board actual get disturb...Therefore I want to "CLONE" it ...but when I try to do

    PieceArray.clone it doesn't clone my PieceArray and that is what I am asking. hope that make more sense and make it clear to you as well..

    Big Smile



  • MrGTI

    Truthfully I can't actually see what you are trying to do.

    As I stated above to clone an array simply call the Clone method. Why are you trying to avoid using it

  • sinu

    Even though you are passing the array as ByVal, only the reference to the class is being copied, so you are still referring to the same object.

    For value types (ie integers, floats, structures, etc) the entire object is copied when using ByVal. However for reference types (ie classes and arrays) only the reference to the object is copied.

    To make the function use a copy of the array, simply call the Clone() method on the array before passing it to NameofFunction.

  • ByVal or ByRef