类初始化的数组 [英] Array of Class initializing

查看:85
本文介绍了类初始化的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


你好,



我在主要中定义了一个类数组程序,当我尝试使用它时出错。



主程序中的定义是

Hallo,

I have defined one array of class in the main program and I get an error when I try to use it.

The definition in the main program is

public const int NumColumns = 10;
public static GridViewComp[] gridView = new GridViewComp[NumColumns];



GridViewComp类包含:string Title,int Width,bool Show。



在另一种形式中,我尝试从XML文件中分配值


GridViewComp Class contains: string Title, int Width, bool Show.

In another form, I try to assign the values from an XML file

private void AssignGridSetup()
{
    var columns = XDocument.Load(Program.setupFileName).Element("Setup").Element("Columns");
    int iLoop = 0;
    foreach (GridViewComp myComp in Program.gridView)
    {
        var item = columns.Element(Program.columnElement[iLoop]);
        myComp.Title = item.Element("Title").Value;
        myComp.Width = Convert.ToInt32(item.Element("Width").Value);
        myComp.Show = Convert.ToBoolean(item.Element("Show").Value);
        iLoop++;
    }
}



(xml部分工作正常)



当我执行代码时,我会出现以下错误:

对象引用未设置为对象的实例行中


(the xml part works fine)

When I execute the code, I become the following error:
"Object reference not set to an instance of an object" in the line

myComp.Title = item.Element("Title").Value;





我找了一个解决方案,我应该初始化数组的每个元素,包括初始化指令



I have looked for a solution, and I should initialize every element of the array, including the initialize instruction

private void AssignGridSetup()
{
    var columns = XDocument.Load(Program.setupFileName).Element("Setup").Element("Columns");
    int iLoop = 0;
    foreach (GridViewComp myComp in Program.gridView)
    {
        var item = columns.Element(Program.columnElement[iLoop]);
        myComp = new GridViewComp();
        myComp.Title = item.Element("Title").Value;
        myComp.Width = Convert.ToInt32(item.Element("Width").Value);
        myComp.Show = Convert.ToBoolean(item.Element("Show").Value);
        iLoop++;
    }
}





但是当我这样做时,编译器显示以下错误:

myNamespace.GridViewComp.GridViewComp()由于其保护级别而无法访问

并标记带下划线的部分。



我也是尝试使用for循环而不是foreach



But when I do so, the compiler shows the following error:
myNamespace.GridViewComp.GridViewComp() is inaccessible due to its protection level
and marks the underlined part.

I also tried with a "for" loop instead of "foreach"

private void AssignGridSetup()
{
    var columns = XDocument.Load(Program.setupFileName).Element("Setup").Element("Columns");
    for (int iLoop = 0; iLoop < Program.NumColumns; iLoop++ )
    {
        var item = columns.Element(Program.columnElement[iLoop]);
        Program.gridView[iLoop] = new GridViewComp();
        Program.gridView[iLoop].Title = item.Element("Title").Value;
        Program.gridView[iLoop].Width = Convert.ToInt32(item.Element("Width").Value);
        Program.gridView[iLoop].Show = Convert.ToBoolean(item.Element("Show").Value);
    }
}



但结果是一样的。



有没有人知道如何用xml数据填充数组,或者我可能做错了什么?



非常感谢您提前。< br $> b $ b

大卫

(PS:我用的是MS VS2010)


but the result is the same.

Has anybody any idea of how can I fill the array with the xml data, or what I could be doing wrong?

Thank you very much in advance.

David
(PS: I use MS VS2010)

推荐答案

你好,



异常是因为未分配对象值而您正在使用该索引。



制作在访问索引之前,确保你有一个带有填充信息的gridView对象。



希望这有助于您找出问题。



谢谢

-Amit
Hi,

Exception is because Object value is not assigned and you are using from that index.

Make sure you have gridView object with filled information before accessing its index.

hope this help you to identify the issue.

thanks
-Amit


最后,我得到了一个解决方案:



Finally, I got one solution:

private void AssignGridSetup()
{
    var columns = XDocument.Load(Program.setupFileName).Element("Setup").Element("Columns");
    for (int iLoop = 0; iLoop < Program.NumColumns; iLoop++ )
    {
        var item = columns.Element(Program.columnElement[iLoop]);
        Program.gridView[iLoop] = new GridViewComp(
            item.Element("Title").Value, 
            Convert.ToInt32(item.Element("Width").Value), 
            Convert.ToBoolean(item.Element("Show").Value));
    }
}





感谢大家的评论。



David



Thank you everybody for your comments.

David


最后,我得到了一个解决方案:



必须在圆括号内初始化值,而不是初始化数组,然后分配值。



Finally, I got one solution:

The values must be initialized within the round brackets, instead of initialize the array and later on assign the values.

private void AssignGridSetup()
{
    var columns = XDocument.Load(Program.setupFileName).Element("Setup").Element("Columns");
    for (int iLoop = 0; iLoop < Program.NumColumns; iLoop++ )
    {
        var item = columns.Element(Program.columnElement[iLoop]);
        Program.gridView[iLoop] = new GridViewComp(
            item.Element("Title").Value, 
            Convert.ToInt32(item.Element("Width").Value), 
            Convert.ToBoolean(item.Element("Show").Value));
    }
}





感谢大家的评论。



David



Thank you everybody for your comments.

David


这篇关于类初始化的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆