Problem with this function..
-
I'm doing this piece of code which should copy the non-null elements in btnArray to the tempArray. Afterwards it should copy the elements of tempArray back to btnArray in order to get rid of the null elements and restructure the array. However i'm getting the following exception: "Specified argument was out of the range of valid values." "Parameter name: Index was out of range. Must be non-negative and less than the size of the collection." I can ofcourse interpret this, but i can't see where i'm making the mistake, i even tried doing the calculations manually and they seem to fit.. or else i'm doing something wrong :confused: If anyone could point out the mistake i'm making i'd be very thankful :)
private void CopyArray() { try { if(btnArray.Count > 0) { int iCounter = 0; for (int iButtonIndex = btnArray.Count; iButtonIndex > 0; iButtonIndex--) { if (btnArray[iButtonIndex-1] != null) { tempArray[iCounter] = btnArray[iButtonIndex-1]; iCounter++; } } for (int iButtonIndex = 0; iButtonIndex < tempArray.Count; iButtonIndex++) { btnArray[iButtonIndex] = tempArray[iButtonIndex]; } } else { MessageBox.Show("(COPY) ARRAY IS EMPTY"); } } catch(Exception ex) { MessageBox.Show("ERROR: " + ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error,MessageBoxDefaultButton.Button1); } }
-
I'm doing this piece of code which should copy the non-null elements in btnArray to the tempArray. Afterwards it should copy the elements of tempArray back to btnArray in order to get rid of the null elements and restructure the array. However i'm getting the following exception: "Specified argument was out of the range of valid values." "Parameter name: Index was out of range. Must be non-negative and less than the size of the collection." I can ofcourse interpret this, but i can't see where i'm making the mistake, i even tried doing the calculations manually and they seem to fit.. or else i'm doing something wrong :confused: If anyone could point out the mistake i'm making i'd be very thankful :)
private void CopyArray() { try { if(btnArray.Count > 0) { int iCounter = 0; for (int iButtonIndex = btnArray.Count; iButtonIndex > 0; iButtonIndex--) { if (btnArray[iButtonIndex-1] != null) { tempArray[iCounter] = btnArray[iButtonIndex-1]; iCounter++; } } for (int iButtonIndex = 0; iButtonIndex < tempArray.Count; iButtonIndex++) { btnArray[iButtonIndex] = tempArray[iButtonIndex]; } } else { MessageBox.Show("(COPY) ARRAY IS EMPTY"); } } catch(Exception ex) { MessageBox.Show("ERROR: " + ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error,MessageBoxDefaultButton.Button1); } }
Where is tempArray created ? You'd do better to shove them into an arraylist, then call the ToArray method to get out an array that is the right size. Christian Graus - Microsoft MVP - C++
-
I'm doing this piece of code which should copy the non-null elements in btnArray to the tempArray. Afterwards it should copy the elements of tempArray back to btnArray in order to get rid of the null elements and restructure the array. However i'm getting the following exception: "Specified argument was out of the range of valid values." "Parameter name: Index was out of range. Must be non-negative and less than the size of the collection." I can ofcourse interpret this, but i can't see where i'm making the mistake, i even tried doing the calculations manually and they seem to fit.. or else i'm doing something wrong :confused: If anyone could point out the mistake i'm making i'd be very thankful :)
private void CopyArray() { try { if(btnArray.Count > 0) { int iCounter = 0; for (int iButtonIndex = btnArray.Count; iButtonIndex > 0; iButtonIndex--) { if (btnArray[iButtonIndex-1] != null) { tempArray[iCounter] = btnArray[iButtonIndex-1]; iCounter++; } } for (int iButtonIndex = 0; iButtonIndex < tempArray.Count; iButtonIndex++) { btnArray[iButtonIndex] = tempArray[iButtonIndex]; } } else { MessageBox.Show("(COPY) ARRAY IS EMPTY"); } } catch(Exception ex) { MessageBox.Show("ERROR: " + ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error,MessageBoxDefaultButton.Button1); } }
-
The tempArray and ButtonArray comes from here in the main form
ButtonArray btnArray; ButtonArray tempArray; <...> private void Form1_Load(object sender, System.EventArgs e) { btnArray = new ButtonArray(this); tempArray = new ButtonArray(this); }
Both ButtonArrays come from this classusing System; namespace ButtonArray { /// /// Summary description for ButtonArray. /// public class ButtonArray : System.Collections.CollectionBase { private readonly System.Windows.Forms.Form HostForm; public System.Windows.Forms.Button AddNewButton() { // Create a new instance of the Button class. System.Windows.Forms.Button aButton = new System.Windows.Forms.Button(); // Add the button to the collection's internal list. this.List.Add(aButton); // Add the button to the controls collection of the form // referenced by the HostForm field. HostForm.Controls.Add(aButton); // Set intial properties for the button object. aButton.Top = Count * 25; aButton.Left = 100; aButton.Tag = this.Count; aButton.Text = "Button " + this.Count.ToString(); aButton.Click += new System.EventHandler(ClickHandler); return aButton; } public ButtonArray(System.Windows.Forms.Form host) { HostForm = host; this.AddNewButton(); } public System.Windows.Forms.Button this [int Index] { get { return (System.Windows.Forms.Button) this.List[Index]; } set { this.List[Index] = value; } } public void RemoveX(int iRemoveX) { // Check to be sure there is a button to remove. if (this.Count > 0) { // Remove the button indexed with the value of iRemoveX HostForm.Controls.Remove(this[iRemoveX-1]); this.List.RemoveAt(this.Count-1); } } public void RemoveButton() { // Check to be sure there is a button to remove. if (this.Count > 0) { // Remove the last button added to the array from the host form // controls collection. Note the use of the indexer in accessing // the array. HostForm.Controls.Remove(this[this.Count -1]); this.List.RemoveAt(this.Count -1); } } public void FlushArray() { // Check to be sure there is a button to remove. if (this.Count > 0) { for(int iButtonIndex = this.Count; iButtonIndex > 0; iButtonIndex--) { // Remove the button indexed with the value of iRemoveX HostForm.Controls.Re