C# 姓名、身份证号码和学校编号控制无效

C# 姓名、身份证号码和学校编号控制无效,c#,loops,C#,Loops,我使用C,我有一个函数来检查这个值。姓名、身份证号码和学校号码 我的记录在文本文件中 如果两个或两个以上相同的名字和姓氏在文本文件中的身份号码和学校号码不同,我需要显示所有这些 我怎样才能用loop完成它 Note=identityBox是我的文本框名称和其中的标识号。这段代码只运行一条记录。这是identityFounder代码。创始人姓名代码与此相同。我将使用“下一步”和“上一步”按钮查看所有记录。这是C代码。请帮帮我。多谢各位 void identityFounder()

我使用C,我有一个函数来检查这个值。姓名、身份证号码和学校号码

我的记录在文本文件中

如果两个或两个以上相同的名字和姓氏在文本文件中的身份号码和学校号码不同,我需要显示所有这些

我怎样才能用loop完成它

Note=identityBox是我的文本框名称和其中的标识号。这段代码只运行一条记录。这是identityFounder代码。创始人姓名代码与此相同。我将使用“下一步”和“上一步”按钮查看所有记录。这是C代码。请帮帮我。多谢各位

    void identityFounder()
    {
        int inout = 0;
        double identityNo = Convert.ToDouble(founderBox.Text);

        String[] line = File.ReadAllLines("C:\\OgrenciBilgisi_v2.txt");

        for (int i = 0; i < line.Length; i++)
        {
            if (line[i].Contains(identityNo.ToString()))
            {
                temp = i;
                inout = 1;
                break;
            }

            else
            {
                inout = 0;
                continue;
            }
        }

        if (inout == 1)
        {
            name.Text = line[temp - 3];
            surname.Text = line[temp - 2];
            address.Text = line[temp - 1];
            identity.Text = line[temp];
            school.Text = line[temp + 1];
            number.Text = line[temp + 2];
            faculty.Text = line[temp + 3];
            deparrtment.Text = line[temp + 4];
            class.Text = line[temp + 5];
        }

        else 
        {
            MessageBox.Show("Record cannot found file.","Warning",MessageBoxButtons.OK
                ,MessageBoxIcon.Information);
        }
    }

首先,有几个简短的旁白:

inout应该是bool,而不是int。 你可以放下else块;它什么也没做。 现在,更广泛地说,您要做的是在解析记录时跟踪您已经看到的名称。如果您同时解析所有标识,这实际上会更快更容易。因为您的行不是按顺序构造的,所以唯一的方法是使用状态机

我只需要制作一个小的控制台应用程序来解析文本文件,寻找重复的名称,如果发现了,就报告文件名或将整个文件打印到屏幕上或其他任何地方;我不清楚你说的“把它们全部展示”是什么意思。可能是这样的:

struct Record
{
    public string Name, Surname;
    public int Identity, School;
}
class Program
{
    static void Main(string[] args)
    {
        const string path = @"C:\OgrenciBilgisi_v2.txt";
        var position = 0;
        var record = new Record();
        var records = new Dictionary<string, Record>(); // key is the concatenation of name and surname

        using (var reader = new StreamReader(path))
        {
            var line = reader.ReadLine();
            if (line == "---") // TODO use your record delimiter here
            {
                // start of new record. compare the existing one and commit it if all fields have been written to
                if (position == 9)
                {
                    // compare records
                    var key = record.Name + record.Surname;
                    if (records.ContainsKey(key))
                    {
                        // this is a duplicate student name. confirm that the id and school # are different
                        if (records[key].Identity == record.Identity &&
                            records[key].School == record.School)
                        {
                            // id and school are the same, so this is an entirely duplicate record
                        }
                        else
                        {
                            // id and school are different, but name and surname are the same
                            // TODO: show all records
                            return;
                        }
                    }
                    else
                    {
                        records.Add(key, record);
                    }
                }
                else
                {
                    Console.Error.WriteLine("Not all fields in record. Malformed data.");
                }

                position = 0;
                record = new Record();
            }
            else
            {
                switch (position)
                {
                    case 0:
                        record.Name = line;
                        break;
                    case 1:
                        record.Surname = line;
                        break;
                    case 3:
                        int id;
                        if (int.TryParse(line, out id))
                        {
                            record.Identity = id;
                        } // else there's an error
                        break;
                    case 4:
                        int school;
                        if (int.TryParse(line, out school))
                        {
                            record.School = school;
                        } // else there's an error
                        break;
                }
                position++;
            }
        }
    }
}

这假设您的数据格式非常好;添加错误检查留给读者作为练习:

您可以从文本文件中发布一行示例吗?这与文本框有什么关系?如果我们可以使用LINQ,那么查找和显示副本应该足够简单,可以吗?另外,如果这些都是文本框,我们应该如何显示多个项目?@Justin R。你为什么想要这个?我可以拍一张截图。有关详细信息,请参见文本文件上的“我的记录类型”。这是SS->@BradleyDotNET表格文本框中的所有记录。我将用按钮更改文本框文本。谢谢您的回答,先生,它起作用了:@Justin R。很抱歉我的不尊重。