C# 创建字段数组

发布于 2024-10-26 02:38:48 字数 3937 浏览 5 评论 0原文

嘿! 我想创建一个字段数组。但是我的代码返回以下错误:字段“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);
            }
        }
    }
}

Hey!
I want to create an array of fields. however my code return an error of the following: Field 'WindowsFormsApplication1.Form1.fieldArray' is never assigned to, and will always have its default value null.

any suggestion to how I can solve this error?

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);
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

左岸枫 2024-11-02 02:38:48

列表可能比数组更容易,但是:您将项目分配给空数组;一旦你知道你需要的数字,首先创建数组:

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

但是,我个人会使用一个列表:

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

A list might be easier than an array, but: you are assigning items to a null array; once you know the number you need, create the array first:

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

However, personally I'd use a list:

private readonly List<Field> fields = new List<Field>();
...
fields.Add(someField);
儭儭莪哋寶赑 2024-11-02 02:38:48

你永远不会将 fieldArray 初始化

//Change
private Field[] fieldArray;

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

You never initialize fieldArray

//Change
private Field[] fieldArray;

to

private Field[] fieldArray = new Field[10];
一身软味 2024-11-02 02:38:48

您从未初始化过fieldArray。表单构造函数中类似 fieldArray = new Field[10]; 的内容应该可以做到这一点。

You never initialized fieldArray. Something like fieldArray = new Field[10]; in the constructor of your Form should do it.

浅忆流年 2024-11-02 02:38:48

在尝试访问 fieldArray 中的元素之前,您必须像这样初始化数组:

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

但是,请小心创建一个足够大的数组来存储所有字段。假设您创建一个包含 5 个元素的 Field[5] 数组,并且尝试为 fieldArray[5] 赋值,您将收到 OutOfBounds 异常。

Before you try to access elements in the fieldArray you have to initialize the array like so:

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

However, be careful to create an array large enough to store all your fields. Suppose you create a Field[5] array of 5 elements, and you try to assign a value to fieldArray[5] you will get an OutOfBounds exception.

向地狱狂奔 2024-11-02 02:38:48

在执行初始化数组每个元素的循环之前,您需要初始化数组本身:

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。

Before doing your loop where you initialize each element of the array, you need to initialize the array itself:

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

On a different note, you're never actually setting fieldArray[0] - I suspect your code that is explicitly setting fieldArray[1].XXX should be working on element 0.

惯饮孤独 2024-11-02 02:38:48

声明数组时初始化它:

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

Initialize your array when you declare it:

private Field[] fieldArray = new Field[100]; // size == 100
柒夜笙歌凉 2024-11-02 02:38:48

我没有看到任何分配数组的行:就像

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

如果字段数量是动态的,我会使用 ArrayList,例如

List fieldArray = new List();

然后向其中添加元素:

fieldArray.Add(...)

I don't see any line that assigns the array: like

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

if the number of fields is dynamic I would use an ArrayList, like

List fieldArray = new List();

and then add elements to it:

fieldArray.Add(...)
新一帅帅 2024-11-02 02:38:48

您必须初始化您的数组

fieldArray = new Field[10];

You must initialize your array

fieldArray = new Field[10];
薄凉少年不暖心 2024-11-02 02:38:48
fieldArray[i] = new Field();

上面的代码让你认为数组已经初始化,但实际上它还没有。您需要像下面这样的东西来为数组分配一些内存。

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

The above code makes you think that the array is already initialized, but actually it has not. You need to have something like the following to allocate some memory for the array.

fieldArray = new Field[/*length or size*/];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文