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. Problems with Array being readonly (HELP)

Problems with Array being readonly (HELP)

Scheduled Pinned Locked Moved C#
helpgraphicsdockerdata-structuresquestion
7 Posts 2 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

    Below i have posted the code for my little project, my problem is located at the line looking like this "tempArray[iCounter++] = btnArray[iButtonIndex];" and i'm getting this error: Property or indexer 'ButtonArray.ButtonArray.this[int]' cannot be assigned to -- it is read only What i'm trying to do is this, once i have removed an object from btnArray, i want to copy the remaining stuff of btnArray to tempArray in order to re-arrange the elements. This should be done through the CopyArray(). However i can't figure out why im getting a readonly error, can anyone please help me out? Thanks alot in advance! ----------------------------------------------------------------- CODE FOR FORM1.CS BELOW ----------------------------------------------------------------- using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using ButtonArray; namespace ButtonArray { /// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button btnAdd; private System.Windows.Forms.Button btnRemove; private System.Windows.Forms.Button btnRemoveX; private System.Windows.Forms.TextBox txtRemoveX; public int iRemoveX; // Declare a new ButtonArray object. ButtonArray btnArray; ButtonArray tempArray; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.btnAdd = new System.Windows.Forms.Button(); this.btnRemove = new System.Windows.Forms.Button(); this.btnRemoveX = new System.Windows.Forms.Button(); this.txtRemoveX = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // //

    G 1 Reply Last reply
    0
    • Y Yannielsen

      Below i have posted the code for my little project, my problem is located at the line looking like this "tempArray[iCounter++] = btnArray[iButtonIndex];" and i'm getting this error: Property or indexer 'ButtonArray.ButtonArray.this[int]' cannot be assigned to -- it is read only What i'm trying to do is this, once i have removed an object from btnArray, i want to copy the remaining stuff of btnArray to tempArray in order to re-arrange the elements. This should be done through the CopyArray(). However i can't figure out why im getting a readonly error, can anyone please help me out? Thanks alot in advance! ----------------------------------------------------------------- CODE FOR FORM1.CS BELOW ----------------------------------------------------------------- using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using ButtonArray; namespace ButtonArray { /// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button btnAdd; private System.Windows.Forms.Button btnRemove; private System.Windows.Forms.Button btnRemoveX; private System.Windows.Forms.TextBox txtRemoveX; public int iRemoveX; // Declare a new ButtonArray object. ButtonArray btnArray; ButtonArray tempArray; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.btnAdd = new System.Windows.Forms.Button(); this.btnRemove = new System.Windows.Forms.Button(); this.btnRemoveX = new System.Windows.Forms.Button(); this.txtRemoveX = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // //

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      Please only post the code that is relevant for the question. --- b { font-weight: normal; }

      Y 1 Reply Last reply
      0
      • G Guffa

        Please only post the code that is relevant for the question. --- b { font-weight: normal; }

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

        // Declare a new ButtonArray object. ButtonArray btnArray; ButtonArray tempArray; btnArray = new ButtonArray(this); tempArray = new ButtonArray(this); private void CopyArray() { try { if(btnArray.Count > 0) { int iCounter; for (int iButtonIndex = 0; iButtonIndex < btnArray.Count; iButtonIndex++) { if (btnArray[iButtonIndex] != null) { tempArray[iCounter++] = btnArray[iButtonIndex]; iCounter++; } } } else { MessageBox.Show("(COPY) ARRAY IS EMPTY"); } } catch(Exception ex) { MessageBox.Show("ERROR: " + ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error,MessageBoxDefaultButton.Button1); } } ---------------------------------------------------------------- CODE FOR BUTTONARRAY.CS BELOW ---------------------------------------------------------------- using System; namespace 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]; } } public void Remove(int iRemoveX) { // 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. // Remove the button indexed with the value of iRemoveX HostForm.Controls.Remove(this[iRemoveX]); this.List.RemoveAt(this.Count -1); } } public void ClickHandler(Object sender, System.EventArgs e) { System.Windows.Forms.MessageBox.Show("You have clicked button " + ((System.Windows.Forms.Button) sender).Tag.ToString()); } } }

        G 1 Reply Last reply
        0
        • Y Yannielsen

          // Declare a new ButtonArray object. ButtonArray btnArray; ButtonArray tempArray; btnArray = new ButtonArray(this); tempArray = new ButtonArray(this); private void CopyArray() { try { if(btnArray.Count > 0) { int iCounter; for (int iButtonIndex = 0; iButtonIndex < btnArray.Count; iButtonIndex++) { if (btnArray[iButtonIndex] != null) { tempArray[iCounter++] = btnArray[iButtonIndex]; iCounter++; } } } else { MessageBox.Show("(COPY) ARRAY IS EMPTY"); } } catch(Exception ex) { MessageBox.Show("ERROR: " + ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error,MessageBoxDefaultButton.Button1); } } ---------------------------------------------------------------- CODE FOR BUTTONARRAY.CS BELOW ---------------------------------------------------------------- using System; namespace 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]; } } public void Remove(int iRemoveX) { // 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. // Remove the button indexed with the value of iRemoveX HostForm.Controls.Remove(this[iRemoveX]); this.List.RemoveAt(this.Count -1); } } public void ClickHandler(Object sender, System.EventArgs e) { System.Windows.Forms.MessageBox.Show("You have clicked button " + ((System.Windows.Forms.Button) sender).Tag.ToString()); } } }

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          You have no set method (only get method) on the default indexer (this[]) property. --- b { font-weight: normal; }

          Y 1 Reply Last reply
          0
          • G Guffa

            You have no set method (only get method) on the default indexer (this[]) property. --- b { font-weight: normal; }

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

            I see.. would you happen to have a suggestion on how to make this set method? :) Thanks alot for the help so far

            G 1 Reply Last reply
            0
            • Y Yannielsen

              I see.. would you happen to have a suggestion on how to make this set method? :) Thanks alot for the help so far

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              It's just the reverse of the get method. set { this.List[Index] = value; } --- b { font-weight: normal; }

              Y 1 Reply Last reply
              0
              • G Guffa

                It's just the reverse of the get method. set { this.List[Index] = value; } --- b { font-weight: normal; }

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

                Thanks alot for the help!

                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