Results 1 to 12 of 12

Thread: [RESOLVED] Changing one varible is causing another varible on another form to change...

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    102

    Resolved [RESOLVED] Changing one varible is causing another varible on another form to change...

    On a form called "ShiftOSDesktop.vb" I have a variable set as follows:
    Public skintitlebar(2) As Image

    On a form called "Shifter.vb" I have another variable set as follows:
    Public shifterskintitlebar(2) As Image

    The variable on the shifter form is meant to act as a preview so you can change it and see the results of the change in the shifter preview. The issue is that setting the value of the shifter variable to nothing and disposing it is also setting the value of the real ShiftOSDesktop variable to nothing and disposing of it too. The shifter variable images are disposed in a form called "Colour_Picker.vb" and the code is as follows:
    Code:
    Case "Title Bar Colour"
                    Shifter.titlebarcolour = pnlnewcolour.BackColor
                    If Shifter.shifterskintitlebar(0) Is Nothing Then  Else Shifter.shifterskintitlebar(0).Dispose()
                    If Shifter.shifterskintitlebar(1) Is Nothing Then  Else Shifter.shifterskintitlebar(1).Dispose()
                    If Shifter.shifterskintitlebar(2) Is Nothing Then  Else Shifter.shifterskintitlebar(2).Dispose()
                    Shifter.shifterskintitlebar(0) = Nothing
                    Shifter.shifterskintitlebar(1) = Nothing
                    Shifter.shifterskintitlebar(2) = Nothing
                    Shifter.shifterskinimages(3) = ""
                    Shifter.shifterskinimages(4) = ""
                    Shifter.shifterskinimages(5) = ""
    The simple act of doing that is causing the "skintitlebar" variable on ShiftOSDesktop.vb to also be wiped which is not what I want.

    How is this possible when I am not even touching it... The only way they are linked in any way is when the Shifter form loads it grabs the values of skintitlebar from ShiftOSDesktop.vb like this:
    Code:
          Array.Copy(ShiftOSDesktop.skintitlebar, shifterskintitlebar, shifterskintitlebar.Length)
    I've been trying to fix this but instead I've wasted 8 hours achieving nothing. I'm at rock bottom now and ultra depressed.

    Can anyone put me out of my misery and shed some light on this?

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Changing one varible is causing another varible on another form to change...

    That's because they hold images, which are an object or reference type (as opposed to a value type like an integer or a date) ... so what actually happens is the pointer to the image gets copied... NOT the image itself. In other words, you still only have one physical copy of the image in memory, you just now have two pointers to it. So when you destroyed the one image, you destroyed the only copy of it.

    Think of the image as a house... and the arrays as two envelopes addressed to that house. Both point to that house. If I were to then destroy the house because one letter was a court order to do so ... the reference of the other letter to that address is also affected. The house is still destroyed.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    102

    Re: Changing one varible is causing another varible on another form to change...

    So how could I separate them into two separate houses? How do I stop them referring to the same object and instead copy it over as a separate object?

    Edit: If I just store a string of text referencing to the location of the image on the computer would that slow the performance down. Lets say I need to quickly load up 20 images in half a second. Would it be faster to load them from a direct bunch of image files on the computer or a bunch of images in variables?
    Last edited by 12padams; Aug 4th, 2014 at 08:56 AM.

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

    Re: Changing one varible is causing another varible on another form to change...

    Normally, I would say that you should load two separate instances, but since you are talking about images, you have to also consider how big they are. Images can be huge, so keeping two copies in memory may mean that you are using an inordinate amount of memory for holding the images.

    Why do you dispose of the image in any case? Load it one time, add the reference to both arrays, but rather than disposing of the image, can you just remove the reference from one of the arrays while leaving the other one alone?
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    102

    Re: Changing one varible is causing another varible on another form to change...

    The original file gets is removed so I need to dispose of the image in order to release the file from use so it can be deleted. So are you saying that I should not copy the image over to another variable and instead load it into memory of that new variable by setting the new variable to load from the image file itself rather than copying the image from another image variable? If that makes sense...

  6. #6
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Changing one varible is causing another varible on another form to change...

    Yeah, basically that's what we're saying, but that poses another issue... you won't be able to delete it until it's freed up from both arrays.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Changing one varible is causing another varible on another form to change...

    If you want to make a true copy of the image, to free up the file, just use the .Clone method to create a new copy. You can then dispose of the original and the file if necessary.

  8. #8
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Changing one varible is causing another varible on another form to change...

    Actually, that might not be a bad idea from the start... load the image, clone it into the first array, then release the file, and not have to worry about the dispose when you delete. To copy to the second array, use the .Clone method again... then you can do what ever you want to the first arry.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    102

    Re: Changing one varible is causing another varible on another form to change...

    Ah so clone is basically what I thought copy was doing. So just replacing the word "copy" with "clone" will fix my problem and store them as two different images like I originally wanted? Awesome I'll try it out in 10 hours when I am back at my computer.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    102

    Re: Changing one varible is causing another varible on another form to change...

    So Initially I tried the following code but it didn't work:
    Code:
    shifterskintitlebar = ShiftOSDesktop.skintitlebar.Clone
    I then tried this code and it worked!
    Code:
    shifterskintitlebar(0) = ShiftOSDesktop.skintitlebar(0).Clone
            shifterskintitlebar(1) = ShiftOSDesktop.skintitlebar(1).Clone
            shifterskintitlebar(2) = ShiftOSDesktop.skintitlebar(2).Clone
    Thanks so much for the help!

    P.S If i had an array I wanted to clone with 1000 images I definitely wouldn't want 1,000 lines of code. Is there one line of code that can clone an entire array. Array.Clone doesn't seem to exist...
    Last edited by 12padams; Aug 5th, 2014 at 03:26 AM.

  11. #11
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Changing one varible is causing another varible on another form to change...

    I definitely wouldn't want 1,000 lines of code.
    Neither would I!

    Is there one line of code that can clone an entire array
    One line? No, but you can do it in 3....
    Code:
            For a = 0 To shifterskintitlebar.Length - 1
                skintitlebar(a) = shifterskintitlebar(a).Clone
            Next
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    102

    Re: Changing one varible is causing another varible on another form to change...

    I feel silly for not thinking of that... Thanks so much! Problem solved!

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