3

I am using this loop to go through my database, and check if players Club field matches the clubAway variable. At if statement it "goes berzerk".

Baza = dataset

Players = table in database

Club = int field

int clubAway

I've checked the values with messagebox, and it says for example: Club = 1 and IDclub = 2.. But it enters the if statement anyway. I have exactly the same for loop, with difference that clubAway is clubHome and it works fine.

for (int o = 0; o <= 10; o++)
{
    for (int p = 1; p <= Baza.Players.Count; p++)
    {
        if (Baza.Players[p - 1 + o].Club == clubAway.IDclub)
        {//do something
        }
    }
}
9
  • 6
    "goes bezerk" isn't a good description for what happens. Please see tinyurl.com/so-hints. I'd expect an exception, given that p - 1 + o can certainly be out of the range of Baza.Players. Commented Jan 29, 2012 at 17:11
  • 1
    do you have something like Baza.Players[p++] or something with o++ in the watch window as these will change the values when stepping through code. Commented Jan 29, 2012 at 17:11
  • @JonSkeet he kind of explains what happens- it ignores the if condition and enters the body of the if statement anyway. Commented Jan 29, 2012 at 17:12
  • You might add some more context, but from what I see it looks like you have a single array - why are you looping twice? Commented Jan 29, 2012 at 17:13
  • What is the purpose of your o variable? A little clarification there, and I can probably help you. Commented Jan 29, 2012 at 17:13

4 Answers 4

4

I am not clear on the purpose of o, but maybe this code will set you on a better path:

var players = Baza.Players.Where(player => player.Club == clubAway.IDclub).Take(11);

    foreach(var p in players){
        //do something with p
    }

UPDATE

In reading the comments below the original post, I deduce that you are simply trying to find the first instance in the set that matches the clubAway value. If that is correct, then one of these code snippets would be more appropriate (depending on your business rules):

var player = Baza.Players.FirstOrDefault(player => player.Club == clubAway.IDclub);

OR

var players = Baza.Players;
foreach(var _player in players){
     var player = _player.FirstOrDefault(p=> p.Club == clubAway.IDclub);
     //Now do something with player.
}

This will return the "player" entity. To use it you would do this (for example):

var playerTeamName = player.teamName;

Of course, I have no knowledge of your actual model, so your code will look different but will follow that pattern.

Good luck!

Sign up to request clarification or add additional context in comments.

3 Comments

As far as I can see it the o is for simulating a LINQ Take(11) after the Where.
Hmm. It did the same thing. I put the foreach loop into for loop that runs 11 times. (Thanks for the post, I learned that trick now)
DinoVelić--I think that @Tobias has a point, you seem to be simulating something that is possible with a single LINQ query and subsequent loop. I am going to edit the code to reflect this and it may help you further, depending on your business rules.
2

The problem is that yo are accessing an illegal element in the collection. The last valid index into Baza.Players is Baza.Players.Count - 1 yet you are using p - 1 + o. In this case p can equal Baza.Players.Count and o can be 10.

You need to change the guard condition on the for loop.

for (int p = 1; p - 1 + o < Baza.Players.Count; p++)

2 Comments

This is clearly a bug, but it doesn't explain why his debugger is entering the if block despite an apparent violation of that if's condition.
@ChrisShain I believe the OP is looking at the wrong index and I'm hoping this will shed light on that.
0

Chances are your code is not compiling correctly, and you are running the 'last good version'. From the Build menu, select "Clean", then try again.

I am guessing that the source code for the binary that the debugger is connecting to is not the same code that you are seeing in your IDE. This can happen if you select "Run last good version" when you start debugging, or if you attach to an already running process launched from a location other than your build output folder.

Comments

0

There's something wrong in the logic of your loop.

Let's say that initially o = 0 and p = 1 (first iteration of the first loop and first iteration of the second loop), then p-1+o equals 0, then o = 0 and p = 2 (first iteration of the first loop and second of the second loop) and p-1+o equals 1. However later when o = 1 (second iteration of the first loop) and p = 1 (first iteration of the second loop), then p-1+o again equals 1, so you're performing a task on the same item really (Baza.Players[1] for no apparent reason).

I hope this solves your problem.

1 Comment

I understand what you're saying. But I use the same loop just a line before this one, and it does the thing that I want (fills the int array with 1-11 numbers. And this loop gives me an int array with all the numbers 23)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.