Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用数组循环_C#_Arrays_Winforms_Visual Studio 2010 - Fatal编程技术网

C# 使用数组循环

C# 使用数组循环,c#,arrays,winforms,visual-studio-2010,C#,Arrays,Winforms,Visual Studio 2010,我在foreach循环上有一个数组: StreamReader reader = new StreamReader(Txt_OrigemPath.Text); reader.ReadLine().Skip(1); string conteudo = reader.ReadLine(); string[] teste = conteudo.Split(new[] { '*' }, StringSplitOptions.RemoveEmptyEntries); foreac

我在foreach循环上有一个数组:

StreamReader reader = new StreamReader(Txt_OrigemPath.Text);
reader.ReadLine().Skip(1);
string conteudo = reader.ReadLine();            
string[] teste = conteudo.Split(new[] { '*' }, StringSplitOptions.RemoveEmptyEntries);

foreach (string s in teste)
{
    string oi = s;
}
我正在读的这行文章包含了几个字段,比如入学、身份证、身份依赖、生日。。。 我有一个勾选列表框,用户在其中选择他想要选择的字段以及他想要的顺序,根据这个选择并知道数组中每个值的顺序,就像我知道第一个是入学考试,第二个是id,第三个是姓名一样,我如何选择一些字段,将其值传递给某个变量,并根据checkedlistbox的顺序对其排序?希望我能说清楚

我试过这个:

using (var reader = new StreamReader(Txt_OrigemPath.Text))
            {
                var campos = new List<Campos>();
                reader.ReadLine();
                while (!reader.EndOfStream)
                {
                    string conteudo = reader.ReadLine();
                    string[] array = conteudo.Split(new[] { '*' }, StringSplitOptions.RemoveEmptyEntries);
                    var campo = new Campos
                    {
                        numero_carteira = array[0]
                    };
                    campos.Add(campo);
                }
            }
Skip1将跳过reader.ReadLine返回的第一行字符串的第一个字符。由于reader.ReadLine本身跳过第一行,因此Skip1是完全多余的

首先创建一个可以存储字段的类

public class Person
{
    public string Matriculation { get; set; }
    public string ID { get; set; }
    public string IDDependent { get; set; }
    public string Birthday { get; set; }

    public override string ToString()
    {
        return String.Format("{0} {1} ({2})", ID, Matriculation, Birthday);
    }
}
这里,为了简单起见,我使用了字符串,但也可以使用int和DateTimes,这需要一些转换

现在,创建一个存储人员的列表

var persons = new List<Person>();

using语句确保StreamReader在完成时关闭。

您认为reader.ReadLine.Skip1;做因为它真的让人烦透了。你一开始想要完成什么?不,一点也不清楚。我知道您希望显示文件中的项目,并且您有一个checkedlistbox来选择和排序项目。由于列表中有项目,您只需将列表中的项目与checkedlistbox中的项目进行比较,并创建一个新列表,以正确的顺序选择项目,然后将数据放入您的输出ListView中?@Antonie-希望这是一个输入错误,但无论如何,他正在读取并丢弃每个文件中的第一行。@DavidHope不在每个文件中。。。它只是跳过了文件的第一行。。。我该如何比较这些呢?我试过了,但没能成功=\@OliverJacot Descombes我不想删除空条目,因为如果我发现空条目是用户想要的某个字段,我会替换其他字段,你知道吗?我现在就试试奥利弗,它说的是无效的表达式术语“字符串为什么?”@Ghaleon:我直接在StackOverflow答案框中键入了代码,而没有在Visual Studio中进行测试。在VisualStudio中,我发现了两个错误。1:使用线末端缺少一个。2:它不应该读取字符串[xy],而应该读取预科中的teste[xy]=teste[0]等等。修正了答案。您使用了字符串拆分选项StringSplitOptions.RemoveEmptyEntries;所以我说你想删除空条目。是的,谢谢。。。最后一件事,我怎么能命令他们在streamwriter上写字呢?我需要将这些字段与用户从CheckedListBox中选择的字段进行比较。因为如果我再次引用person类,如果您重写person类的ToString方法,则值将为空。请参阅上面的“我的编辑”以返回要在CheckedListBox中显示的内容,只需将其DataSource属性设置为persons列表,即可将所有人员添加到CheckedListBox中:myCheckedListBox.DataSource=persons;。现在您可以通过:person p=PersonmyCheckedListBox.SelectedItem;获取实际的人员;。或者如果myCheckedListBox.GetItemCheckedi{Person p=PersonmyCheckedListBox.Items[i];}
var persons = new List<Person>();
using (var reader = new StreamReader(Txt_OrigemPath.Text)) {
    reader.ReadLine();  // Skip first line (if this is what you want to do).
    while (!reader.EndOfStream) {
        string conteudo = reader.ReadLine();
        string[] teste = conteudo.Split('*');
        var person = new Person {
            Matriculation = teste[0],
            ID = teste[1],
            IDDependent = teste[2],
            Birthday = teste[3]
        };
        persons.Add(person);
    }
}