2017-06-15 51 views
0

我正在爲大學做一個賦值,並且裏面有一個名爲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代碼

+0

能否請你告訴代碼? –

+0

'Public Calculation(ListBox lb) { InitializeComponent(); lstDisplay = lb; theCalcs = new ArrayList(); theCalcs [0] = 0; }' 這是計算類的構造函數。 – Raynking11

+0

當我在AddStripForm(MainForm)中引用類時,這是代碼 計算calc = new Calculation(lstDisplay); 這是我得到的錯誤 – Raynking11

回答

0

由於你正在使用的ListBox這是一個實例變量收到這個錯誤。移動計算類初始化到構造如下:

Calculation calc; 

public AddStripForm() 
{ 
    InitializeComponent(); 
    calc = new Calculation(lstDisplay); 
} 
+0

是的,你是對的.. – Abhishek

0

我能夠使用此代碼複製錯誤。問題是LstDisplay是一個實例成員,但您嘗試訪問是通過使用TypeName,AddStripForm.LstDisplay。相反,請從構造函數中提供值。

public AddStripForm() 
{ 
    InitializeComponent(); 
    calc = new Calculation(LstDisplay); 
} 

代碼複製錯誤

namespace StackOverFlow { 

    public class AddStripForm : Form { 
     public ListBox LstDisplay { get; set; } 
    } 

    public class Calculation { 

     public Calculation(ListBox lb) { 
     } 
    } 

    internal class Program { 

     Calculation calc = new Calculation(AddStripForm.LstDisplay); 

     private static void Main(string[] args) { 
     } 
    } 
} 
0

的問題是,成員變量lstDisplay是在相同的類中的成員變量,計算的是在(AddStripForm)的成員變量,而該計算值被初始化,並使用該類之前的成員變量lstDisplay已完全構建。

在構造函數中賦值calc。

calc = new Calculation(lstDisplay); 

考慮:

public class WontCompile 
{ 
    int a; 
    int b; 
    int c = a + b; 
} 

public class WillCompile 
{ 
    int a; 
    int b; 
    int c; 

    public WillCompile() 
    { 
     c = a + b; 
    } 
} 
相關問題