JCreationsInc Posted July 9, 2003 Posted July 9, 2003 Welll hello again it is I mogore the great with an array problem for you people to digest. I have searched far and wide in the ever confusing world of the VS.Net Combined Help to no avail. And it is now that I bow down to behold the true power of the dotnetforums! Anyways heres what is happening. I have and array of type Object, and I need to make an exhact copy of it to work on without disturbing the original array (and this is where I run into a brick wall at full speed) I am using the Array.Copy function and it seems to work except when I alter the array, the origianl copy that I do not want altered gets altered. Here is my code: 'The items array mentioned is a multi-dimensional object array 'with each element initialized to a different class that holds 'properties of items such as controls and images 'Copy our items array into our working array Dim ItemsClone(Items.GetUpperBound(0)) As Object Items.Copy(Items, ItemsClone, Items.GetUpperBound(0)) According to the help, a reference type array is being copied instead of a value type array(which is what I want) so when I alter the "ItemsClone" array it alters the original "Items" array(which is what I dont want) Here is a portions from the help file: "If sourceArray and destinationArray are both reference-type arrays or are both arrays of type Object, a shallow copy is performed. A shallow copy of an Array is a new Array containing references to the same elements as the original Array. The elements themselves or anything referenced by the elements are not copied. In contrast, a deep copy of an Array copies the elements and everything directly or indirectly " So... Does anyone know how to create a "Deep" copy? or do I need to use a another Type for my array instead of Object? Quote When in doubt.... use the sledge Hammer!!
*Experts* mutant Posted July 9, 2003 *Experts* Posted July 9, 2003 Try this: Dim ItemsClone() As Object = Items.Clone() Quote
JCreationsInc Posted July 9, 2003 Author Posted July 9, 2003 Thats a negative The original items array is still being changed when you alter the ItemsClone array, I have also tried the Copy and CopyTo functions and the same thing occurrs. :confused: Quote When in doubt.... use the sledge Hammer!!
JCreationsInc Posted July 10, 2003 Author Posted July 10, 2003 How this thing works I will explain the program in detail so everybody will have a better understanding of how the array that is giving me problems is set up, incase I am doing somthing wrong. Well anyways I am creating a program that will allow me or anyone who uses my applications to easily create "Skins" for my applications. It functions like a WYSIWYG editor and allows you to position and set the properties of the controls on the window to match the theme of your Skin. So it works like this, there is a file I call it the "Master Skin File" the application reads a directory where the Master Skin Files are installed and loads the available files into a ComboBox. Then the user selects a Program(depending on the available master skin files) to create a project. Then the application proceeds to read the selected Master Skin File to determine how many dimensions will be needed for the Items Array that will eventually hold all the properties for the controls in the project. The Items List is initialized to 2 dimensions. The first dimension contains the number of windows as stated in the master skin file. Then another function determines the type of the each control and initializes the array element within the current window dimension to the a Custom Class that will hold all the properties (such as position and color) for that control. 'I have a couple of classes the hold properties of various items such as 'ComboBoxes, TextBoxes and Images. 'First I declare the Items Dim Items() as Object 'Here is an Example of the ComboBox Properties Class: <Serializable()> Public Class ComboBox_Properties ' Class declarations Private vBackColor As Color Private vForeColor As Color Private vType As String 'Hold the name of the combobox for later use Public Property ItemType() As String Get ItemType = vType End Get Set(ByVal Value As String) vType = Value End Set End Property 'Holds the backcolor of the control Public Property BackColor() As Color Get BackColor = vBackColor End Get Set(ByVal Value As Color) vBackColor = Value End Set End Property End Class 'My Master Skin File reading routine determines which array 'element must be assigned to which Class. Items(Window)(ControlNum) = New ComboBox_Properties() Now lets say the user has finished designing the window and want to test it, so he/she will invoke the routine that will create the skin file and test it within the window. In this routine I need to make an exact copy of the array to change the image filenames to something more appropiate. This is where I hit a roadblock because the cloned array is created as a reference type array and when I change the clone array it changes the origianal array. I have tried Copy and CopyTo, and Clone functions and nothing seems to work. If anybody has any ideas they would be greatly appreciated, thanks! Quote When in doubt.... use the sledge Hammer!!
*Experts* jfackler Posted July 10, 2003 *Experts* Posted July 10, 2003 Dim aray() As String ={"jon", "cindy", "nicholas", "alex", "nathan"} Dim newarray(5) As String Array.Copy(aray, newarray, 5) newarray(0) = "fred" Debug.WriteLine(newarray(0).ToString()) 'result will be fred Debug.WriteLine(aray(0).ToString) 'result will still be jon This method produces a new array with the elements in the source array. Changing the elements in the new array does not affect the source arrays elements. What you were looking for? Jon Quote
JCreationsInc Posted July 10, 2003 Author Posted July 10, 2003 What I am having a problem with is after I copy the array and change a property of the copied array, it also changes the original array which I do not want. I think it is due to the array being an object array but I am not sure. Quote When in doubt.... use the sledge Hammer!!
*Experts* jfackler Posted July 10, 2003 *Experts* Posted July 10, 2003 Re: How this thing works 'I have a couple of classes the hold properties of various items such as 'ComboBoxes, TextBoxes and Images. 'First I declare the Items Dim Items() as Object Dim TestItems() as Object 'Here is an Example of the ComboBox Properties Class: <Serializable()> Public Class ComboBox_Properties ' Class declarations Private vBackColor As Color Private vForeColor As Color Private vType As String 'Hold the name of the combobox for later use Public Property ItemType() As String Get ItemType = vType End Get Set(ByVal Value As String) vType = Value End Set End Property 'Holds the backcolor of the control Public Property BackColor() As Color Get BackColor = vBackColor End Get Set(ByVal Value As Color) vBackColor = Value End Set End Property End Class 'My Master Skin File reading routine determines which array 'element must be assigned to which Class. Items(Window)(ControlNum) = New ComboBox_Properties() Array.Copy(Items, TestItems, Items.GetUpperBound(0)) 'Now TestItems contains all the 'elements of Items but changing the elements of TestItems will not effect the elements of Items. ' Use TestItems where you would use Items to test the skin. B] Quote
JCreationsInc Posted July 10, 2003 Author Posted July 10, 2003 nope still dont work oh well screw it ill just do it the hard way, I guess you cant copy object arrays. It says in the help that if you copy an object array it will be copied as a reference type array. Oh well... Quote When in doubt.... use the sledge Hammer!!
Gizmo001 Posted July 10, 2003 Posted July 10, 2003 May be I don't know what I am talking about. But why don't you define another array of the same type. Make it equal to your array. Won't the two array be then distinct from each other? Quote
Gizmo001 Posted July 10, 2003 Posted July 10, 2003 I started with one textbox (textbox1), generated two arrays obj1 and obj2, defined obj1, then made obj2's accessible role same as obj1's. Seemed to work. Obj1 was unaffected by what I did to Obj2. Is this what you are trying to do? Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim Obj1(3), obj2(3) As TextBox, i As Integer For i = 0 To 3 Obj1(i) = New TextBox() Obj1(i).AccessibleRole = TextBox1.AccessibleRole Obj1(i).Left = 200 + 100 * i Obj1(i).Top = 200 Obj1(i).Text = "testing" Controls.Add(Obj1(i)) Obj1(i).Visible = True Obj1(i).BackColor = Color.Green Obj1(i).ForeColor = Color.White Next i For i = 0 To 3 obj2(i) = New TextBox() obj2(i).AccessibleRole = Obj1(i).AccessibleRole obj2(i).Left = 200 + 100 * i obj2(i).Top = 300 obj2(i).Text = "tested" Controls.Add(obj2(i)) obj2(i).Visible = True obj2(i).BackColor = Color.Red obj2(i).ForeColor = Color.White Next i End Sub Quote
JCreationsInc Posted July 10, 2003 Author Posted July 10, 2003 wholly crap you got it! wow I never knew about that property thanks man!!! Quote When in doubt.... use the sledge Hammer!!
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.