我正在爲大學做一個賦值,並且裏面有一個名爲Calculation的類,作爲簡介的一部分,它的構造函數中帶有一個ListBox的Calculation類。 我收到一個錯誤,說'字段初始值設定項不能引用非靜態字段,方法或屬性'AddStripForm.lstDisplay' 我不確定這意味着在這種情況下。 任何幫助,將不勝感激。構造函數中的列表框
namespace GUI_Calculator
{
public partial class Calculation : Form
{
AddStripForm addStrip = new AddStripForm();
bool isModified = false;
ListBox lstDisplay;
ArrayList theCalcs;
string filename = "";
public Calculation(ListBox lb)
{
InitializeComponent();
lb = lstDisplay;
theCalcs = new ArrayList();
theCalcs[0] = 0;
}
public void Add(CalcLine c1)
{
}
public void Clear()
{
foreach(int n in theCalcs)
{
theCalcs[n] = null;
}
lstDisplay.Items.Clear();
}
public void Redisplay()
{
}
public CalcLine Find(int n)
{
return (CalcLine)theCalcs[n];
}
public void Replace(CalcLine newCalc, int n)
{
isModified = true;
theCalcs[n] = newCalc;
Redisplay();
}
public void Insert(CalcLine newCalc, int n)
{
}
public void Delete(int n)
{
foreach(int x in theCalcs)
{
if(Convert.ToInt32(theCalcs[x]) == n)
{
theCalcs[x] = null;
}
}
}
public void SaveToFile(string filename)
{
}
public void LoadFromFile(string filename)
{
}
}
}
有計算類
namespace GUI_Calculator
{
public partial class AddStripForm : Form
{
Calculation calc = new Calculation(lstDisplay);
public AddStripForm()
{
InitializeComponent();
}
private void txtValue_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
string newNum = txtValue.Text;
if (txtValue.Text.StartsWith("+"))
{
lstDisplay.Items.Add(txtValue.Text);
}
else if (txtValue.Text.StartsWith("-"))
{
lstDisplay.Items.Add(txtValue.Text);
}
else if (txtValue.Text.StartsWith("*"))
{
lstDisplay.Items.Add(txtValue.Text);
}
else if (txtValue.Text.StartsWith("/"))
{
lstDisplay.Items.Add(txtValue.Text);
}
else if (txtValue.Text.StartsWith("="))
{
lstDisplay.Items.Add(txtValue.Text);
}
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
private void btnInsert_Click(object sender, EventArgs e)
{
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void btnDelete_Click(object sender, EventArgs e)
{
lstDisplay.SelectedItems.Clear();
}
}
}
還有就是AddStripForm代碼
能否請你告訴代碼? –
'Public Calculation(ListBox lb) { InitializeComponent(); lstDisplay = lb; theCalcs = new ArrayList(); theCalcs [0] = 0; }' 這是計算類的構造函數。 – Raynking11
當我在AddStripForm(MainForm)中引用類時,這是代碼 計算calc = new Calculation(lstDisplay); 這是我得到的錯誤 – Raynking11