2011-03-24 78 views
0

嘿! 我想創建一個字段數組。但是,我的代碼返回以下錯誤:字段'WindowsFormsApplication1.Form1.fieldArray'永遠不會分配給,並且將始終將其默認值爲null。C#創建字段數組

任何建議我如何解決這個錯誤?

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.IO; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     private Field[] fieldArray; 
     private Field f; 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void populateTree(string path, TreeNode tv1) 
     { 
      string[] dir = Directory.GetDirectories(path); 
      foreach (string d in dir) 
      { 
       string entry = Path.GetFileName(d); 
       TreeNode t = tv1.Nodes.Add("Folder", entry, 0); 
       populateTree(d, t); 
      } 
      string[] files = Directory.GetFiles(path); 
      foreach (string f in files) 
      { 
       string entry = Path.GetFileName(f); 
       tv1.Nodes.Add(f, entry, 1); 
      } 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      //populate the tree 
      TreeNode t = treeView1.Nodes.Add("Units"); 
      populateTree(@"..\units\", t); 

      f = new Field(); 

      for (int i = 0; i < 10; i++) 
      { 
       fieldArray[i] = new Field(); 
      } 

      fieldArray[1].label.AutoSize = true; 
      fieldArray[1].label.Location = new System.Drawing.Point(323, 9); 
      fieldArray[1].label.Name = "Programtittle"; 
      fieldArray[1].label.Text = "UAI UnitDef Editor"; 
      this.Controls.Add(fieldArray[1].label); 

      int clabel = 36; 
      //fieldArray[1].varName = new string[] { "unitName", "name", "description" }; //define labels 

      //popluate label 
      for (int i = 1; i < fieldArray[i].varName.Length; i++) 
      { 
       fieldArray[i].label = new Label(); 
       fieldArray[i].label.AutoSize = true; 
       fieldArray[i].label.Location = new System.Drawing.Point(323, clabel); 
       fieldArray[i].label.Name = "label"; 
       this.Controls.Add(fieldArray[i].label); 
       fieldArray[i].label.Text = fieldArray[i].varName[i]; 
       clabel = clabel + 26; 
      } 

      //populate textbox 
      int cbox = 33; 
      for (int i = 0; i < fieldArray[i].varName.Length; i++) 
      { 

       fieldArray[i].txtBox = new TextBox(); 
       fieldArray[i].txtBox.Location = new System.Drawing.Point(380, cbox); 
       fieldArray[i].txtBox.Name = "txtBox"; 
       fieldArray[i].txtBox.Size = new System.Drawing.Size(100, 50); 
       this.Controls.Add(fieldArray[i].txtBox); 

       cbox = cbox + 26; 
      } 
     } 

     private void populateLabelTxtBox(string path) 
     { 
      //f.txtBox.Multiline = true; //added for testing purpose; 

      //read,split file 
      string text = System.IO.File.ReadAllText(path); 

      char[] delimiters = new char[] { '{', '=', ';', '}' }; 
      string[] parts = text.Split(delimiters, StringSplitOptions.RemoveEmptyEntries); 

      for (int i = 0; i < parts.Length; i++) 
      { 
       fieldArray[i].txtBox.Text = parts[i]; 
      } 
     } 

     private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) 
     { 
      if (treeView1.SelectedNode.Name != "Folder") 
      { 
       string text = System.IO.File.ReadAllText(treeView1.SelectedNode.Name); 
       //f.txtBox.Text = text; 
       populateLabelTxtBox(treeView1.SelectedNode.Name); 
      } 
     } 
    } 
} 
+0

嘿!我嘗試了你一直建議的!但是在fieldArray [0] .label = new Label();它會返回一個錯誤,指出對象引用未設置爲對象的實例。有什麼建議麼? – New27 2011-03-25 01:05:31

回答

2

你永遠不會初始化fieldArray

//Change 
private Field[] fieldArray; 

private Field[] fieldArray = new Field[10]; 
2

你永遠不會初始化fieldArray。在您的窗體的構造函數中,像fieldArray = new Field[10];應該這樣做。

1

之前您嘗試訪問的元素在fieldArray你必須初始化數組,像這樣:

fieldArray = new Field[/*size of the array*/]; 

但是,要小心創造足夠大的存儲所有領域的數組。假設你創建一個由5個元素組成的Field [5]數組,並且你試圖給fieldArray [5]賦值,你將得到一個OutOfBounds異常。

0

初始化您的數組,當你把它聲明:

private Field[] fieldArray = new Field[100]; // size == 100 
1

做你的循環,你初始化數組中的每個元素之前,你需要初始化數組本身:

fieldArray = new Field[10]; // Create this with the appropriate size 
for (int i = 0; i < 10; i++) 
{ 
    fieldArray[i] = new Field(); 
} 

在不同的注意,你從來沒有真正設置fieldArray[0] - 我懷疑你的代碼明確設置fieldArray[1].XXX應該在元素0上工作。

3

列表可能比數組更容易,但是:將項目分配給空數組;一旦你知道你所需要的數量,首先創建數組:

fieldArray = new Field[10];       
for (int i = 0; i < 10; i++) 
{...} 

不過,我個人會使用一個列表:

private readonly List<Field> fields = new List<Field>(); 
... 
fields.Add(someField); 
0

我沒有看到該數組分配任何行:像

int number_of_elements = 100; 
fieldArray = new Field[number_of_elements]; 

如果字段的數目是動態的,我會用一個ArrayList,像

列表fieldArray =新列表();

,然後添加元素到它:

fieldArray.Add(...) 
0

您必須初始化數組

fieldArray = new Field[10]; 
0
fieldArray[i] = new Field(); 

上面的代碼使你認爲該陣列已經被初始化,但實際上它有不。你需要像下面這樣爲數組分配一些內存。

fieldArray = new Field[/*length or size*/];