Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi,

I am working on a code

C#
int a = 0, b = 1, c = 2, d = 3;

                    if (select1 == "Red")
                        busOneDetails = msgDetailsOfBus(SCANLA.Run.BusDetails[a]);
                    if (select1 == "Yellow")
                        busOneDetails = msgDetailsOfBus(SCANLA.Run.BusDetails[b]);
                    if (select1 == "Green")
                        busOneDetails = msgDetailsOfBus(SCANLA.Run.BusDetails[c]);
                    if (select1 == "Orange")
                        busOneDetails = msgDetailsOfBus(SCANLA.Run.BusDetails[d]);



                    if (select2 == "Red")
                        busTwoDetails = msgDetailsOfBus(SCANLA.Run.BusDetails[a]);
                    if (select2 == "Yellow")
                        busTwoDetails = msgDetailsOfBus(SCANLA.Run.BusDetails[b]);
                    if (select2 == "Green")
                        busTwoDetails = msgDetailsOfBus(SCANLA.Run.BusDetails[c]);
                    if (select2 == "Orange")
                        busTwoDetails = msgDetailsOfBus(SCANLA.Run.BusDetails[d]);



User may select any of the 4 colors red,yellow,green,orange

I need to write a code in a way, if user selcts red and yellow then I need to take remaining two colors and work out on them.

suppose if the user selects Red and Yellow then I need to take
SCANLA.Run.BusDetails[c],SCANLA.Run.BusDetails[d] and work on them

How can I write code to disregard the user selected colors???


Thanks
John
Posted
Updated 4-Feb-14 2:26am
v2
Comments
[no name] 4-Feb-14 8:38am    
Is SCANLA a listbox???

C#
string[] userInput = {"red", "orange"};
string[] colors = {"red", "yellow", "green", "orange"};
for (int i = 0; i < colors.Length; i++)
{
  if (userInput.Contains(colors[i]) == false)
  {
   var busDetails = msgDetailsOfBus(SCANLA.Run.BusDetails[i]);
   // post processing of bus details here (for example, adding busDetails to list of details
  }
}
 
Share this answer
 
C#
for (int i = 0; i < 3; i++)
{
  if (msgDetailsOfBus(SCANLA.Run.BusDetails[i] != busOneDetails  & msgDetailsOfBus(SCANLA.Run.BusDetails[i] != busTwoDetails)
  {
   BusDetails busDetails = msgDetailsOfBus(SCANLA.Run.BusDetails[i]);
   .
   .
   .
  }
}
 
Share this answer
 
Here's an illustration of the general principle of returning a List of objects not in some other List, using Linq. You'll have to adapt it to your work:
C#
List<Color> clrList = new List<Color>{Color.Red, Color.Yellow, Color.Green, Color.Orange};

private List<Color> selectOtherColors(List<Color> selectedColors)
{
   if(selectedColors.Count != 2) throw new ArgumentException("Must supply two Colors !");

   return clrList.Where((clr, ndx) => ! selectedColors.Contains(colorList[ndx])).ToList();
}
Test:
C#
var useTheseColors = selectOtherColors(new List<Color>{Color.Red, Color.Yellow});
This will return a List<Green,Orange>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900