Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problem with this function..

Problem with this function..

Scheduled Pinned Locked Moved C#
helpcssdatabasedata-structures
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    Yannielsen
    wrote on last edited by
    #1

    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); } }

    C M 2 Replies Last reply
    0
    • Y Yannielsen

      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); } }

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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++

      1 Reply Last reply
      0
      • Y Yannielsen

        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); } }

        M Offline
        M Offline
        miah alom
        wrote on last edited by
        #3

        Can you show us the code where you declared tempArray.

        Y 1 Reply Last reply
        0
        • M miah alom

          Can you show us the code where you declared tempArray.

          Y Offline
          Y Offline
          Yannielsen
          wrote on last edited by
          #4

          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 class using 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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups