Problems with Array being readonly (HELP)
-
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(); // //
-
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(); // //
-
// 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()); } } }
-
// 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()); } } }
-
You have no set method (only get method) on the default indexer (this[]) property. --- b { font-weight: normal; }
I see.. would you happen to have a suggestion on how to make this set method? :) Thanks alot for the help so far
-
I see.. would you happen to have a suggestion on how to make this set method? :) Thanks alot for the help so far
-
It's just the reverse of the get method.
set { this.List[Index] = value; }
--- b { font-weight: normal; }Thanks alot for the help!