mercredi 7 octobre 2020

How do I make multiple if conditions, that returns true, execute in C#?

I have a checkedListBox where the user can select whatever they want to update. I want them to be able to freely update 1 up to 5 characteristics of a machine. So when they only want to update 1 thing, they do not have to provide the other 4 characteristics. Also, when they want to update 5 characteristics, they can do it in one go. For that purpose I have the following if statements:


if (clbCharacteristicsToUpdate.CheckedItems.Count != 0)
{
                        if (clbCharacteristicsToUpdate.GetSelected(0))
                        {
                            currentITime = Convert.ToDouble(tbCurrentITime.Text);
                            MessageBox.Show(currentITime.ToString());
                            dh.UpdateCurrentITime(machineNr, currentITime);
                        }
                        if (clbCharacteristicsToUpdate.GetSelected(1))
                        {
                            cycleTime = Convert.ToDouble(tbCycleTime.Text);
                            MessageBox.Show(cycleTime.ToString());
                            dh.UpdateCycleTime(machineNr, cycleTime);
                        }
                        if (clbCharacteristicsToUpdate.GetSelected(2))
                        {
                            nrOfLinesPerCm = Convert.ToInt32(tbNrOfLinesPerCm.Text);
                            MessageBox.Show(nrOfLinesPerCm.ToString());
                            dh.UpdateNrOfLinesPerCm(machineNr, nrOfLinesPerCm);
                        }
                        if (clbCharacteristicsToUpdate.GetSelected(3))
                        {
                            heightOfLamallae = Convert.ToDouble(tbHeightOfLamallae.Text);
                            MessageBox.Show(heightOfLamallae.ToString());
                            dh.UpdateHeightOfLamallae(machineNr, heightOfLamallae);
                        }
                        if (clbCharacteristicsToUpdate.GetSelected(4))
                        {
                            if (rbLTB.Checked)
                            {
                                machineType = 2;
                                MessageBox.Show(machineType.ToString());
                            }
                            else if (rbSTB.Checked)
                            {
                                machineType = 1;
                                MessageBox.Show(machineType.ToString());
                            }
                            if(!rbLTB.Checked && !rbSTB.Checked)
                            {
                                MessageBox.Show("Select a machine type to update!");
                                return;
                            }
                            dh.UpdateType(machineNr, machineType);  
                         }

}

My problem is that, when I choose and update 1 thing it works perfectly. But when I choose multiple ones, it only executes the last if statement that returns true. I thought about using if-else but then only the first one that returns true will be executed. I also thought about having if statements for each possibility. But since I have 5 characteristics I can update, this would make 25 possibilities and I do not want to have 25 if statements. Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire