Problemer med Array er readonly?!

Tags:    c#

Herunder har jeg postet mit lille projekt, mit problem findes påfølgende linie

"tempArray[iCounter++] = btnArray[iButtonIndex];"

Hvor jeg får denne fejl

Property or indexer 'ButtonArray.ButtonArray.this[int]' cannot be assigned to -- it is read only

Det jeg prøver at gøre er, jeg tager samtlige elementer i btnArray der ikke er NULL og kopierer dem til tempArray for, at re-arrangere dem i tempArray.

Men jeg kan ikke forstå hvorfor jeg får en readonly fejl, er der nogen der kan hjælpe mig?

På forhånd mange tak!

-----------------------------------------------------------------
CODE FOR FORM1.CS BELOW
-----------------------------------------------------------------

<code>using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using ButtonArray;

namespace ButtonArray
{
/// <summary>
/// Summary description for Form1.
/// </summary>
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;

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
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();
//
// btnAdd
//
this.btnAdd.Location = new System.Drawing.Point(192, 8);
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(96, 23);
this.btnAdd.TabIndex = 0;
this.btnAdd.Text = "Add Button";
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// btnRemove
//
this.btnRemove.Location = new System.Drawing.Point(192, 40);
this.btnRemove.Name = "btnRemove";
this.btnRemove.Size = new System.Drawing.Size(96, 23);
this.btnRemove.TabIndex = 1;
this.btnRemove.Text = "Remove Button";
this.btnRemove.Click += new System.EventHandler(this.btnRemove_Click);
//
// btnRemoveX
//
this.btnRemoveX.Location = new System.Drawing.Point(192, 104);
this.btnRemoveX.Name = "btnRemoveX";
this.btnRemoveX.Size = new System.Drawing.Size(96, 23);
this.btnRemoveX.TabIndex = 2;
this.btnRemoveX.Text = "Remove X";
this.btnRemoveX.Click += new System.EventHandler(this.btnRemoveX_Click);
//
// txtRemoveX
//
this.txtRemoveX.Location = new System.Drawing.Point(192, 136);
this.txtRemoveX.Name = "txtRemoveX";
this.txtRemoveX.Size = new System.Drawing.Size(96, 20);
this.txtRemoveX.TabIndex = 3;
this.txtRemoveX.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.txtRemoveX);
this.Controls.Add(this.btnRemoveX);
this.Controls.Add(this.btnRemove);
this.Controls.Add(this.btnAdd);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

btnArray = new ButtonArray(this);
tempArray = new ButtonArray(this);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{

}

private void aButton_Click(object sender, System.EventArgs e)
{

}

private void btnAdd_Click(object sender, System.EventArgs e)
{
// Call the AddNewButton method of MyControlArray.
btnArray.AddNewButton();
// Change the BackColor property of the Button 0.
btnArray[0].BackColor = System.Drawing.Color.Red;
}

private void btnRemove_Click(object sender, System.EventArgs e)
{

if(btnArray.Count > 0)
{
btnArray.Remove(btnArray.Count-1);
}
else
{
MessageBox.Show("ARRAY IS EMPTY");
}// Call the Remove method of MyControlArray.
}

// public void Collapse()
// {
// if(btnArray.Count > 0)
// {
// for(int i = btnArray.Count; i < btnArray.Count; i++)
// {
// if(i+1 <= btnArray.Count)
// btnArray = btnArray[i+1];
// }
// btnArray[btnArray.Count] = null;
// }



/*
public void removeAndCollapse(object[] array, int index)
{
if(array.Length > index)
{
for(int i = index; i < array.Length; i++)
{
if(i+1 <= array.Length)
array = array[i+1];
}

array[array.Length] = null;
}
}
*/

// }

private void btnRemoveX_Click(object sender, System.EventArgs e)
{
try
{
iRemoveX = Convert.ToInt32(txtRemoveX.Text);

if(btnArray.Count > 0)
{
btnArray.Remove(iRemoveX);
}
else
{
MessageBox.Show("(REMOVE) ARRAY IS EMPTY");
}
}

catch(Exception ex)
{
MessageBox.Show("ERROR: " + ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error,MessageBoxDefaultButton.Button1);
}
}

private void CopyArray()
{
try
{
if(btnArray.Count > 0)
{
int iCounter;
for (int iButtonIndex = 0; iButtonIndex < btnArray.Count; iButtonIndex++)
{
// READ ONLY ?!
if (btnArray[iButtonIndex] != null)
{
tempArray[iCounter++] = btnArray[iButtonIndex];
iCounter++;
}
//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);
}
}

private void ShowArray()
{
}
}
}</code>
----------------------------------------------------------------
CODE FOR BUTTONARRAY.CS BELOW
----------------------------------------------------------------
<code>using System;

namespace ButtonArray
{
/// <summary>
/// Summary description for ButtonArray.
/// </summary>
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.
//HostForm.Controls.Remove(this[this.Count -1]);
//this.List.RemoveAt(this.Count -1);

// 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());
}
}
}</code>



1 svar postet i denne tråd vises herunder
1 indlæg har modtaget i alt 1 karma
Sorter efter stemmer Sorter efter dato
Hej Yan,

Dit problem er at din index'er (this[]) er ReadOnly. Dvs du mangler en set sektion.

public System.Windows.Forms.Button this [int Index]
{
get
{
return (System.Windows.Forms.Button) this.List[Index];
}
// Add this section to your code and it will work!
set
{
this.List[Index] = value;
}
}

/Lars

Herunder har jeg postet mit lille projekt, mit problem
findes påfølgende linie

"tempArray[iCounter++] = btnArray[iButtonIndex];"

Hvor jeg får denne fejl

Property or indexer 'ButtonArray.ButtonArray.this[int]' cannot be assigned to -- it is read only

Det jeg prøver at gøre er, jeg tager samtlige elementer i btnArray der ikke er NULL og kopierer dem til tempArray for, at re-arrangere dem i tempArray.

Men jeg kan ikke forstå hvorfor jeg får en readonly fejl, er der nogen der kan hjælpe mig?

På forhånd mange tak!

-----------------------------------------------------------------
CODE FOR FORM1.CS BELOW
-----------------------------------------------------------------

<code>using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using ButtonArray;

namespace ButtonArray
{
/// <summary>
/// Summary description for Form1.
/// </summary>
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;

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
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();
//
// btnAdd
//
this.btnAdd.Location = new System.Drawing.Point(192, 8);
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(96, 23);
this.btnAdd.TabIndex = 0;
this.btnAdd.Text = "Add Button";
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// btnRemove
//
this.btnRemove.Location = new System.Drawing.Point(192, 40);
this.btnRemove.Name = "btnRemove";
this.btnRemove.Size = new System.Drawing.Size(96, 23);
this.btnRemove.TabIndex = 1;
this.btnRemove.Text = "Remove Button";
this.btnRemove.Click += new System.EventHandler(this.btnRemove_Click);
//
// btnRemoveX
//
this.btnRemoveX.Location = new System.Drawing.Point(192, 104);
this.btnRemoveX.Name = "btnRemoveX";
this.btnRemoveX.Size = new System.Drawing.Size(96, 23);
this.btnRemoveX.TabIndex = 2;
this.btnRemoveX.Text = "Remove X";
this.btnRemoveX.Click += new System.EventHandler(this.btnRemoveX_Click);
//
// txtRemoveX
//
this.txtRemoveX.Location = new System.Drawing.Point(192, 136);
this.txtRemoveX.Name = "txtRemoveX";
this.txtRemoveX.Size = new System.Drawing.Size(96, 20);
this.txtRemoveX.TabIndex = 3;
this.txtRemoveX.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.txtRemoveX);
this.Controls.Add(this.btnRemoveX);
this.Controls.Add(this.btnRemove);
this.Controls.Add(this.btnAdd);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

btnArray = new ButtonArray(this);
tempArray = new ButtonArray(this);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{

}

private void aButton_Click(object sender, System.EventArgs e)
{

}

private void btnAdd_Click(object sender, System.EventArgs e)
{
// Call the AddNewButton method of MyControlArray.
btnArray.AddNewButton();
// Change the BackColor property of the Button 0.
btnArray[0].BackColor = System.Drawing.Color.Red;
}

private void btnRemove_Click(object sender, System.EventArgs e)
{

if(btnArray.Count > 0)
{
btnArray.Remove(btnArray.Count-1);
}
else
{
MessageBox.Show("ARRAY IS EMPTY");
}// Call the Remove method of MyControlArray.
}

// public void Collapse()
// {
// if(btnArray.Count > 0)
// {
// for(int i = btnArray.Count; i < btnArray.Count; i++)
// {
// if(i+1 <= btnArray.Count)
// btnArray = btnArray[i+1];
// }
// btnArray[btnArray.Count] = null;
// }



/*
public void removeAndCollapse(object[] array, int index)
{
if(array.Length > index)
{
for(int i = index; i < array.Length; i++)
{
if(i+1 <= array.Length)
array = array[i+1];
}

array[array.Length] = null;
}
}
*/

// }

private void btnRemoveX_Click(object sender, System.EventArgs e)
{
try
{
iRemoveX = Convert.ToInt32(txtRemoveX.Text);

if(btnArray.Count > 0)
{
btnArray.Remove(iRemoveX);
}
else
{
MessageBox.Show("(REMOVE) ARRAY IS EMPTY");
}
}

catch(Exception ex)
{
MessageBox.Show("ERROR: " + ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error,MessageBoxDefaultButton.Button1);
}
}

private void CopyArray()
{
try
{
if(btnArray.Count > 0)
{
int iCounter;
for (int iButtonIndex = 0; iButtonIndex < btnArray.Count; iButtonIndex++)
{
// READ ONLY ?!
if (btnArray[iButtonIndex] != null)
{
tempArray[iCounter++] = btnArray[iButtonIndex];
iCounter++;
}
//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);
}
}

private void ShowArray()
{
}
}
}</code>
----------------------------------------------------------------
CODE FOR BUTTONARRAY.CS BELOW
----------------------------------------------------------------
<code>using System;

namespace ButtonArray
{
/// <summary>
/// Summary description for ButtonArray.
/// </summary>
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.
//HostForm.Controls.Remove(this[this.Count -1]);
//this.List.RemoveAt(this.Count -1);

// 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());
}
}
}</code>





t