Results 1 to 5 of 5

Thread: [RESOLVED] Array is inheriting instead of reseting....

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2011
    Location
    Seattle
    Posts
    218

    Resolved [RESOLVED] Array is inheriting instead of reseting....

    Im having a little issue in my code that I wasnt expecting and Im not quite sure how to fix it.

    normally in an example like this:

    Code:
            Dim test1 As String = "1"
            Dim test2 As String = test1
            test2 = "test"
    
            MsgBox(test1 & test2)
    You would expect the prompt to be "1test" as defining test2 = "test" will not change test1 to "test" giving you the prompt "testtest"

    However, that is exactely what is happening when I use this principal on a two dimensional string array.

    Code:
       Public Shared Sub MakeSchedule(ByVal BUArray As String(,), ByVal unPatList As List(Of String))
            Dim patRemove As New List(Of Integer)
    
            Do Until patRemove.Count = unPatList.Count
                Dim nArray As String(,) = BUArray
                patRemove = New List(Of Integer)
    
                For x as integer = 0 to 8
                       'Do a bunch of code to fill in nArray 
                       'add to PatRemove.count 
                Next
            Loop
        End Sub
    Where the comments are I am doing a series of commands that add to both nArray and patRemove.count. However, sometimes after the commands patRemove.count will still not be high enough (equal to unPatlist.count). On those instances I need patRemove.Count to start back at 0 and for nArray to reinitialize itself from BUArray (BackUp Array). But when it goes to reinitialize, BUArray has been changing alongside nArray isstead of staying with the original data even though I make absolutely no mention anywhere in the code to change BUArray.

    So my question is, why is changing nArray changing BUArray when the only mention of BUArray is when I am setting nArray? wouldnt it work just like the above example with test1 and test2?

    Thank You,
    -Z


    As a side note, I should mention that I can tell BUArray is changing from the following code

    Code:
    sb1 = New StringBuilder
    sb1.AppendLine("Rooms: " & patRemove.Count)
    sb1.AppendLine("AMT")
    For x As Integer = 0 To BUArray.GetUpperBound(1)
         Dim str As String = ""
         For y As Integer = 0 To BUArray.GetUpperBound(0)
               str += BUArray(y, x) & ","
         Next
         sb1.AppendLine(str)
    Next
    Last edited by Zmcpherson; Jan 12th, 2016 at 05:25 PM.

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

    Re: Array is inheriting instead of reseting....

    You have them entangled.

    Note that an array is an object, so it is a reference type. That means that the variable BuArray or nArray don't hold the array, they just hold a reference to the array. In other words, the variables hold the memory address of the array, effectively. When you copy one array to the other, as you do in this case:

    Dim nArray As String(,) = BUArray

    all you are doing is copying the array address from one place to another. After that line, nArray and BUArray both hold copies of the memory address of the array. What you were expecting is that the line would copy the array itself into a new variable, which would be a deep copy. In this case, a deep copy like that wouldn't be terribly difficult, but the compiler can't work that way. It has to work the same in all cases, and deep copies aren't possible at all (or even desirable) in many cases.

    So, to solve the problem, you need to create a new array in nArray that is the size of BUArray and copy the elements from one to the other. There are a variety of ways to do this. The straightforward way is to do those steps exactly. The more roundabout way would be to serialize BUArray to a memory stream, then deserialize it back into nArray. That takes less code, but is also less efficient for this case than simply sizing nArray and copying all the values across.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2011
    Location
    Seattle
    Posts
    218

    Re: Array is inheriting instead of reseting....

    uhhggg, im an idiot.

    Thanks Shaggy, glad to see you're still doing this.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Sep 2011
    Location
    Seattle
    Posts
    218

    Re: [RESOLVED] Array is inheriting instead of reseting....

    I fixed it using

    Code:
            ReDim BUArray(nArray.GetUpperBound(0), nArray.GetUpperBound(1))
            For x As Integer = 0 To nArray.GetUpperBound(0)
                For y As Integer = 0 To nArray.GetUpperBound(1)
                    BUArray(x, y) = nArray(x, y)
                Next
            Next
    Is that what you would of done?

    Thanks,
    -Z

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

    Re: [RESOLVED] Array is inheriting instead of reseting....

    Yeah, I would use the loop in a loop solution unless there is a way with Array.Copy. I'm not sure that Array.Copy works for 2D arrays, but it's really fast.
    My usual boring signature: Nothing

Tags for this Thread

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