繁体   English   中英

加载一系列数组(C#)

[英]Load a range of arrays (C#)

我需要跟踪C#中一个属性的节点中有多少个项目,而且我不知道如何加载从1开始并以变量结尾的数组

这部分的代码是这个

//Loads file containing OSXInstaller File List
doc.Load("Distribution.xml");
XmlNodeList ChoiceNode = doc.GetElementsByTagName("choice");
foreach (XmlNode choices in ChoiceNode)
{
    //Code Executed only  for packages, not categories
    if (choices.HasChildNodes == true)
    {
        choicetitle.Add(choices.Attributes["title"].InnerText);
        //Counts the number of items with an attribute of "title" in the 
        //distributions file, so if the user selects #1 in the list box it will 
        //know what number one is
        choicelistcount++;
    }
    if (choices.HasChildNodes == false)
    {
    }
    else
    {
        MessageBox.Show("Your Mac OS X Installation disk is damaged or was mounted improperly, please try again and doube-check your information!");
    }
}

我试图让它计算带有title属性的项目数,然后能够打印出列表中的所有项目

choicetitle是字符串列表

我希望它看起来像:

listBox1.Items.Add(choicetitle[1-choicetitlecount]);

编辑:回答下面的一些评论;

我在很短的时间内就把它们放在一起,拼写检查之类的东西对我来说并不重要

我之所以包含else语句的原因是,它添加了额外的检查是否已安装OSX安装磁盘

该程序是一种将软件包添加和删除到OSX Installer的工具,而不是像live-cd类型那样,而是将可以安装到新OS X安装网站上的软件包添加到http://osxreformer.org。

您的问题尚不清楚,但您可能想使用List<T>类,该类可以容纳任意数量的元素。

您可以创建List<SomeType> ,然后根据需要SomeType添加SomeType多个SomeType对象,而不必担心大小或容量。 (除非您的内存不足)

您需要遍历列表的一部分,如下所示:

for(int i = 1; i < choicetitlecount; i++)
    listBox1.Items.Add(choicetitle[i]);

请注意,.Net数组和集合都是基于0的,因此,除非要跳过第一项,否则可能要从0开始。(将int i = 1更改为int i = 0


通常,应该给控件赋予有意义的名称(标签除外)。 例如,您可能想将listBox1重命名为choiceTitleList

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM