8 Replies - 313 Views - Last Post: 27 March 2011 - 09:57 AM Rate Topic: -----

#1 EmperorOfMankind  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 27-March 11

Foreach loops

Posted 27 March 2011 - 09:13 AM

I want to use a foreach loop on two different lists.

I currently have:

foreach (Buyer buyer in buyers)


I would like to add "and foreach Seller seller in sellers" to have it all in the same loop. I have tried simply adding the code with the & sign but that does not work. Any help? I'm sure it is quite simple to do.

Thanks

This post has been edited by EmperorOfMankind: 27 March 2011 - 09:14 AM

Is This A Good Question/Topic? 0
  • +

Replies To: Foreach loops

#2 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: Foreach loops

Posted 27 March 2011 - 09:17 AM

you have posted this in the c++ forums. I believe you have the wrong place :)
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,421
  • Joined: 23-August 08

Re: Foreach loops

Posted 27 March 2011 - 09:17 AM

That looks like C#, not C++?

EDIT: Ah, you've been posting in the wrong place! We HAVE a C# Forum. I will move your topics there.

This post has been edited by JackOfAllTrades: 27 March 2011 - 09:19 AM

Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Foreach loops

Posted 27 March 2011 - 09:18 AM

C++ (or C) doesn't have a foreach loop. Use a for, while or do-while loop.
Was This Post Helpful? 0
  • +
  • -

#5 EmperorOfMankind  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 27-March 11

Re: Foreach loops

Posted 27 March 2011 - 09:23 AM

Sorry guys I didn't realize I posted in the wrong section. Yes this is c# not c/c++.
Was This Post Helpful? 0
  • +
  • -

#6 blank_program  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 282
  • Joined: 22-July 09

Re: Foreach loops

Posted 27 March 2011 - 09:29 AM

I am taking a shot in the dark but why not have it some sort of variable collection instead? You could then assign it to match one of your others before the loop and then go through the new collection, or just have three conditonal loops.

Here is some code to illustrate my first idea but I have no idea if it will actually work as I don't have time to test it.

List<Buyer> b = new List<Buyer>();
List<Seller> s = new List<Seller>();

List<object> LoopList = new List<object>();

if(RadioButtonBuyer.Checked == true)
{
LoopList = b;
}

if(RadioButtonSeller.Checked == true)
{
LoopList = s;
}

foreach (object obj in LoopList)
{
//loop logic here
}


This post has been edited by blank_program: 27 March 2011 - 09:29 AM

Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,421
  • Joined: 23-August 08

Re: Foreach loops

Posted 27 March 2011 - 09:30 AM

Be that as it may, I think we need more information to help you. You may be going about this the wrong way from the start.
Was This Post Helpful? 0
  • +
  • -

#8 EmperorOfMankind  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 27-March 11

Re: Foreach loops

Posted 27 March 2011 - 09:35 AM

Ok, this is for an e-auctioning system. I'm using this for the user to log in. My idea was to have the 'username' entered by the user compared to each 'username' in the 'buyers' and 'sellers' lists to see if there's a match.
Was This Post Helpful? 0
  • +
  • -

#9 blank_program  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 282
  • Joined: 22-July 09

Re: Foreach loops

Posted 27 March 2011 - 09:57 AM

View PostEmperorOfMankind, on 27 March 2011 - 09:35 AM, said:

Ok, this is for an e-auctioning system. I'm using this for the user to log in. My idea was to have the 'username' entered by the user compared to each 'username' in the 'buyers' and 'sellers' lists to see if there's a match.

In the code I gave earlier, which may or may not work, change the if statements for some sort of other comparison to your database or where ever you store the login information to get if its the buyer or seller. You might even be able somehow to then use the group in a switch statement to assign the list.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1