喂! 我想创建一个字段数组。但是我的代码返回以下错误:字段'WindowsFormsApplication1.Form1.fieldArray'从未分配给,并且将始终具有其默认值null。
有关如何解决此错误的任何建议?
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 :(得分:3)
列表可能比数组更容易,但是:您将项目分配给空数组;一旦知道了所需的数字,首先创建数组:
fieldArray = new Field[10];
for (int i = 0; i < 10; i++)
{...}
但是,我个人会使用一个列表:
private readonly List<Field> fields = new List<Field>();
...
fields.Add(someField);
答案 1 :(得分:2)
你永远不会初始化fieldArray
//Change
private Field[] fieldArray;
到
private Field[] fieldArray = new Field[10];
答案 2 :(得分:2)
您从未初始化fieldArray
。 <= 1}}在表单的构造函数中应该这样做。
答案 3 :(得分:1)
在尝试访问fieldArray中的元素之前,您必须像这样初始化数组:
fieldArray = new Field[/*size of the array*/];
但是,要小心创建一个足以存储所有字段的数组。假设您创建了一个包含5个元素的Field [5]数组,并尝试为fieldArray [5]赋值,您将获得OutOfBounds异常。
答案 4 :(得分: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上工作。
答案 5 :(得分:0)
声明时初始化数组:
private Field[] fieldArray = new Field[100]; // size == 100
答案 6 :(得分:0)
我没有看到任何指定数组的行:比如
int number_of_elements = 100;
fieldArray = new Field[number_of_elements];
如果字段数是动态的,我会使用ArrayList,比如
列出fieldArray = new List();
然后添加元素:
fieldArray.Add(...)
答案 7 :(得分:0)
您必须初始化数组
fieldArray = new Field[10];
答案 8 :(得分:0)
fieldArray[i] = new Field();
上面的代码让你认为数组已经初始化,但实际上它没有。您需要具有以下内容才能为阵列分配一些内存。
fieldArray = new Field[/*length or size*/];