Results 1 to 5 of 5

Thread: byval array changes when giving value

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    61

    byval array changes when giving value

    hi there,
    i am coming accross i weird problem
    i have this structure
    VB Code:
    1. Public Structure Pack_
    2.         Dim pack() As Byte
    3.         Dim comport As Integer
    4.         Dim toRouter As Boolean
    5.         Dim header As header_
    6.     End Structure

    and declared globaly
    VB Code:
    1. dim pack as pack_
    and initiated at the form_load
    VB Code:
    1. pack = new pack_
    i have this function
    VB Code:
    1. myfunction(byval dim Wpack() as byte)
    2.  
    3. pack.pack =wpack
    4.  
    5. pack.pack(4) = 30
    6. end function

    for some reason, wpack(4) becomes 30 as well...
    i dont get it at all...

  2. #2

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    61

    Re: byval array changes when giving value

    ok i think i got the problem...the pack.pack = wpack doesnt really work.
    I copied the elements of wpack to pack.pack one by one and the problem was solved.
    But still i dont really get it why that happened

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: byval array changes when giving value

    That's exactly what should happen. When you pass a parameter by value it is the variable you cannot change. That means that if you pass an array object by value and you assign a new array object to that variable then that change will not be reflected after the method completes. It doesn't mean that you can't make changes to the object that the variable refers to, so any changes you make to the elements of the array will be reflected after the method completes. If you don't understand the difference between value types and reference types then this may be a bit confusing, but if you do understand that difference then it's perfectly logical.

    What exactly are you trying to do in that method anyway? Are you trying to copy the contents of one array to another and then change one element of the new array? If so then use the Array.Copy method. By this line:
    VB Code:
    1. pack.pack =wpack
    you are saying pack.pack now refers to the same object that wpack refers to. If you don't understand that then think about your mother. Who is your father's wife? Are they two different ways to refer to the same person? Yes they are, and programming is the same.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: byval array changes when giving value

    By the way, don't you get all your various packs confused? I try to avoid naming a member variable to the same name as a variable I might use outside the object.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    61

    Re: byval array changes when giving value

    Ok, thanks a lot!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width