break loop foreach
(8 posts, started )
#1 - ReVoX
break loop foreach
Hi, I have a problem with my loop for check the negative cash, I need break the loop when the action/ban finished.

This is my code, i am using in MSO packets, I tried with return 0; , return false; and break; , but doesn't working.

foreach (clsConnection c in Connections)
{
if (c.Cash < -1000)
{
InSim.Send_MST_Message("/msg " + (Connections[GetConnIdx(MSO.UCID)].Username) + " 100");
//using /msg for test, I know I need replace for /ban
}
}

You should be able to use break to exit out of the loop. When you say it doesn't work, what's actually happening?

foreach (clsConnection c in Connections)
{
if (c.Cash < -1000)
{
InSim.Send_MST_Message("/msg " + (Connections[GetConnIdx(MSO.UCID)].Username) + " 100");
break; // Break out of the foreach loop.
}
}

Of course "break;" works to break the foreach loop, I guess the error is more in the general logic.

1) I don't think you want to actually break the loop, as I believe you want to ban all players with cash < 1000, not only the first one it finds.

2) If you don't actually ban the user (read: remove him from the server) then this code will be repeated again and again (spamming messages respectively ban commands), because each time the check runs it will detect the player with too low cash again. Maybe add an extra flag to the player object so you know you already issued the ban command for him, skipping him in the check:

foreach (clsConnection c in Connections)
{
if (c.Cash < -1000 && !c.Banned)
{
InSim.Send_MST_Message("/ban ...");
c.Banned = true;
}
}

E: or maybe it's better to just create a temporary list of users you want to ban in the loop, and only afterwards execute the "/ban" command for each of these connections, so that if an event for removing the connection out of the list occurs, it doesn't mess with the foreach loop.
#4 - ReVoX
my problem is I need break the flood, us can see my problem:

Please read my post, it solves your problem.

Also
3) Make sure your connection list is properly maintained! It seems like there are many more entries in your list than there are actual connections.
#6 - ReVoX
uhm, but i dont want define a list with c.banned in Clients.cs, only want check for ban, I am seeing for use other packet, for example PLP.
I dont know how to use your code, only create new string or byte in Clients.cs?
rather than doing a loop, why not check on each transaction the amount of cash the user has.

Ie, if they get fined £1000, check if the user has >£1000, if not, ban.
#8 - ReVoX
umh, is a good option , i am going to trying it

Thanks guys!

break loop foreach
(8 posts, started )
FGED GREDG RDFGDR GSFDG