开发者

C# Creating an array of Fields

开发者 https://www.devze.com 2023-02-19 18:05 出处:网络
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 v

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].lab开发者_运维知识库el.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);
            }
        }
    }
}


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


You never initialize fieldArray

//Change
private Field[] fieldArray;

to

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


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


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.


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.


Initialize your array when you declare it:

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


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(...)


You must initialize your array

fieldArray = new Field[10];


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*/];
0

精彩评论

暂无评论...
验证码 换一张
取 消