For loop breaks after one [closed] - c#

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a for loop and it keeps breaking after going around once however I'm unsure on why it is doing this.
for (int fX = 1; fX < 17; fX++)
{
foreach (RoomItem OldItem in Room.GetRoomItemHandler().GetFurniObjects(fX, 26))
{
Logging.WriteLine(OldItem.BaseItem.ToString());
Room.GetRoomItemHandler().RemoveRoomItem(OldItem, false);
}
}
NOTE:
Logging.WriteLine is the same as Console.WriteLine, its just my logging system.
Result of console:
1
But the console should be outputting (1,2,3 and so fourth)
Any ideas? I even tried locking the foreach iteration but that didn't work.

You modify the collection while you iterate through it. This results in undefined behaviour.
You may fix this with using a constant list for the foreach like so:
foreach (RoomItem OldItem in
Room.GetRoomItemHandler().GetFurniObjects(fX, 26).ToList())
{
...
}
But I think you should rather think through your design again.

You should not be changing a collection while iterating through it using a for-loop, re-write using while or do-while.

Related

Is there a way to compare the exact value of a string in C# [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
So I made a simple c# file reader because I was bored and added an if statement to filter the results. But when I ran it, it gave me more results than I wanted. I was supposed to get
276, 2, and there was only one line inside the file with that value, but I got multiple. I checked the file and it had lines ending with the same value. I tried string.Equals(line, "276, 2") but it gave me the same results. I doubt there isn't something in c# that doesn't solve this issue.
You can use Regex, as it's mentioned in this issue
bool result = Regex.IsMatch(line, "\\b276, 2\\b");

C# While won't enter with true condition [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have a bit of a problem with my c# code.
I have a while loop:
while(current.parent != null)
{
solution.Add(current.move);
current = current.parent;
}
All of the variables have values, but for some reason it doesn't enter the while. I placed a break at the while and see that the parent property is not null, but it just skips the entire while.
Any ideas why? or how I can modify this to work?
Because current.parent is null to begin with.
Probably because current is the top most in your tree, i.e. has no children. (Or whatever you want to call it)
The code in your question is very vague but I would guess just using
solution.Add(current.move);
while(current.parent != null)
{
current = current.parent;
solution.Add(current.move);
}
may help.
This may solve the actual problem you are having (other than not knowing how to use a debugger ;))
So here you log the current move always, then log any parent moves.
Maybe look at refactoring this with a do..while loop!!
After seeing that every node in the tree had a null value (but the properties of the node weren't), I changed everything to work with the Node's properties instead, which finally worked.
Thanks guys =)

Index and length must refer to a location with the string. Parameter name: length error [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
My program has been receiving this error recently and I am not sure why. The code that is triggering it is:
foreach (var lot in item.LotNum.Split('|'))
{
string vendor = string.Empty;
if (lot.Trim().Contains("-"))
vendor = lot.Trim().Substring(0, item.LotNum.IndexOf("-"));
}
LotNum = "br549 | BR549 | 570-PRIOR" and lot is "570-PRIOR" (without quotes) when the error triggers. I've not used IndexOf before and so I am not sure what is wrong with the string that is being sent in. I want to check for what causes the error beforehand because the exception is stopping the program and the bad data will be there for a while until it is fixed, and more may be added in the future.
Any help would be appreciated!
New answer according your code update:
var lots = item.LotNum.Split('|');
foreach (var lot in lots)
{
string vendor = string.Empty;
if (lot.Contains("-"))
vendor = lot.Substring(0, lot.IndexOf("-")).Trim();
}
Again, you were using IndexOf for a variable different than the one you want to get a substring
You are using IndexOf for a variable different than the one you want to get a substring, so, the index will be out of range.
Try with: edited
variable = variable.Trim();
int index = variable.IndexOf("-");
if (index > 0)
variable.Substring(0, index);
Another way to do this is to check for the existence of the character using Contains, and if it's found use the IndexOf, otherwise just use the Length of the string (and put Trim at the end to avoid issues with using the Length after trimming):
variable.Substring(0, variable.Contains('-')
? variable.IndexOf('-')
: variable.Length)
.Trim();

items.IndexOf not printing out position [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am fairly new to C# programming and have just finished learning about placeholders and the great convenience of the Array libraries/methods that can be used in C#.
I have written a very simple program that start with a populated array list of 3 strings. I have everything correct, as far as I know, but for whatever reason my items.IndexOf() method is only and always printing out 0 into the console.
Can anyone tell/teach me as to why this may be happening? As far as I know a foreach statement should update the values of the array so each pass through the items.IndexOf() value should change but it is not.
Here is my short code and I appreciate any advice, tips and help!
namespace ConsoleApplication01
{
class Program
{
static void Main(string[] args)
{
string[] items = { "sword", "shield", "potion" };
WriteLine("Quick! You're being attacked by a Goblin Bruiser!");
WriteLine("Which item will you use?");
foreach (string item in items)
{
WriteLine("{0} {1}",items.IndexOf(item), item);
}
ReadLine();
}
}
}
UPDATE:
The issue was they syntax of items.IndexOf and Array.IndexOf.
I assumed that because my items variable was an array it could be used in the way my code below shows. Thank you for all of the people who have taught me !
you have your variables mixed up
it should be items not item in the for loop. like this :
foreach (string item in items)
{
WriteLine("{0} {1}", Array.IndexOf(items,item), item);
}

Which code is written better? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I ran a static code analysis tool on our tool and looking at its results the code below was one of the things it was talking about:
SpreadSnapshot oSnap = new SpreadSnapshot();
using (oSnap.SetRowCol(fpSpread, row, col))
{
SpreadSetComboBox(fpSpread, list, displayProperty);
}
So I changed it to the code below and it fixed the error that the tool was talking about:
using (SpreadSnapshot oSnap = new SpreadSnapshot())
{
oSnap.SetRowCol(fpSpread, row, col);
SpreadSetComboBox(fpSpread, list, displayProperty);
}
So in your opinion Which style of coding do you think is more appropriate and less error-prone?
Thanks
The latter - it ensures that you don't end up using oSnap after the using statement.
Aside from anything else, it would be pretty odd for SetRowCol to return something disposable... what would that even mean?
The two mean completely different things, unless SetRowCol returns this at the end. In the first, you're disposing the results of SetRowCol. In the second, you're disposing the SpreadSnapshot.
If both are disposable, you should do a using for both:
using (SpreadSnapshot oSnap = new SpreadSnapshot())
using (oSnap.SetRowCol(fpSpread, row, col))
{
SpreadSetComboBox(fpSpread, list, displayProperty);
}

Categories

Resources