需要搜索整个数组而不仅仅是一个

时间:2014-03-20 13:58:53

标签: c# arrays string visual-studio-2010 class

很抱歉提出太多问题,但是当我在数组中将字符串与字符串进行比较时,我只会搜索一个数组实例,例如程序允许您将患者添加到数组但是当我使用比较字符串,它只会与最后添加的患者进行比较,你能帮帮我吗?

这是我的计划:

     public class Patient
    {
        public string patientidString;
        public string firstNameString;
        public string lastNameString;
        public string dateString;

        public Patient()
        {
            patientidString = "";
            firstNameString = "";
            lastNameString = "";
            dateString = "";
        }

        public Patient(string idValue, string firstNameVal, string lastNameVal, string dateVal)
        {
            patientidString = idValue;
            firstNameString = firstNameVal;
            lastNameString = lastNameVal;
            dateString = dateVal;
        }

    }


    //Array
    Patient[] patientInfo = new Patient[10];


    private void button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < patientInfo.Length; ++i)
        patientInfo[i] = new Patient();
        try
        {
            foreach (Patient patientinfoIndex in patientInfo)

            patientInfo[itemCountInteger].patientidString = textBox1.Text;
            patientInfo[itemCountInteger].firstNameString = textBox2.Text;
            patientInfo[itemCountInteger].lastNameString = textBox3.Text;
            patientInfo[itemCountInteger].dateString = dateTimePicker1.Text;

            string names = patientInfo[itemCountInteger].patientidString + "  " + patientInfo[itemCountInteger].firstNameString + " " + patientInfo[itemCountInteger].lastNameString;
            listBox1.Items.Add(names);
            itemCountInteger++;
            listBox1.SelectedItem = names;
        }
        catch
        {
            MessageBox.Show("Contacts are limited to 20. Please delete some contacts prior to adding more.");
        }

    }


    //Search Button search a patients id and display his surname in the label if patient is found  display his surname and name
    private void button2_Click(object sender, EventArgs e)
    {

        int intTest = 0;

        for (int x = 0; x < patientInfo.Length; x++)
        {
            if (textBox4.Text == patientInfo[x].patientidString)
            {
                label6.Text = (patientInfo[x].firstNameString + "  " + patientInfo[x].lastNameString);
                PatientForm patientform = new PatientForm();
                patientform.Show();
                patientform.label6.Text = (patientInfo[x].patientidString);
                patientform.label7.Text = (patientInfo[x].firstNameString);
                patientform.label8.Text =(patientInfo[x].lastNameString);
                patientform.dateTimePicker1.Text = (patientInfo[x].dateString);
                intTest = 1;
            }

        }

        if (intTest == 0)
        {
            label6.Text = ("not found");
        }
    }

3 个答案:

答案 0 :(得分:2)

如果您使用的是foreach循环,则无需使用索引器

foreach (Patient patientinfoIndex in patientInfo)
{
    patientinfoIndex.patientidString = textBox1.Text;
    patientinfoIndex.firstNameString = textBox2.Text;
    patientinfoIndex.lastNameString = textBox3.Text;
    patientinfoIndex.dateString = dateTimePicker1.Text;

    string names = 
        patientinfoIndex.patientidString + "  " + 
        patientinfoIndex.firstNameString + " " + 
        patientinfoIndex.lastNameString;

    listBox1.Items.Add(names);
    listBox1.SelectedItem = names;
}

你也缺少大括号{ },这会导致循环只在循环声明后执行第一个语句,而不是其余的。

答案 1 :(得分:1)

我假设你的button1_Click功能被绑定到Add New Patient按钮。

代码行

for (int i = 0; i < patientInfo.Length; ++i)
    patientInfo[i] = new Patient();
每次添加新患者时,

都会清除您之前输入的所有患者数据!这就是为什么你只找到你搜索时输入的最后一位病人。您需要将这些代码行移动到其他位置,例如表单类的构造函数。

答案 2 :(得分:0)

intTest = 1;之后,你需要打破for循环。

intTest = 1;
break;