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

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 =)

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");

Equivalent strings that look exactly the same but arn't equal in Unity [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 1 year ago.
Improve this question
An public string ItemSubType which is assigned a string "primary" by another script is somehow not equal to the string "primary". I've checked for empty characters but there isn't any. I've been absolutly stumped by this but also interested in how this is happening.
Debug.Log("---primary---");
Debug.Log("---" + ItemSubType + "---");
if(ItemSubType != "primary")
{
Debug.Log("This is ridiculous!");
}
Here is logs:A picture of the log
Thank you all for the comments!
After browsing through some other questions, I found .Trim().
Mystically there seems to be an invisible character in the string that isn't a whitespace character, as would be evident by the lack of spaces when adding '---' to either side.
By doing this:
ItemSubType = ItemSubType.Trim();
It fixed the Issue. To give some context, I pulled the string data from a csv file. I checked the file for any extra spaces but there wasn't any. Not sure if this is related tho.

OR not working in C# if statement [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 5 years ago.
Improve this question
I am uploading files to a database and the method checks to make sure the correct file type is uploaded. If i check for each MIME type individually, it works fine, but when placed inside an OR it triggers the error.
if (formFile.ContentType != "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
{
modelState.AddModelError(formFile.Name,
$"The {fieldDisplayName}file ({fileName}) is not a valid file type.");
}
The above works fine, but when i add multiple filetypes(see below), it does not work
if (formFile.ContentType != "application/vnd.openxmlformats-officedocument.wordprocessingml.document" || formFile.ContentType != "text/plain")
{
modelState.AddModelError(formFile.Name,
$"The {fieldDisplayName}file ({fileName}) is not a valid file type.");
}
I know this will be something simple, i am just not seeing it.
The OR is working correctly. The issue is that you want to use a logical AND (&&).
It seems like you made a mistake.
Basic usage
if(True or True) {
// TRUE
}
Your usage
if(!True or !True) {
// False
}

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();

Dictionary key doesn't exist wheras it does exist [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
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.
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.
Improve this question
I use a Dictionary<string, Test> to store some datas, where the key is a file path. To display this dictionary I create a treeview with node names being the key of the corresponding test.
When I try to get back one value, I use:
Test test = Tests[node.Name];
But I throw me that:
The given key was not present in the dictionary.
So I made a few manual debug:
Console.WriteLine(node.Name + Environment.NewLine + Tests.First().Key);
Which outputs:
P:\poolman\LY21\2015\LY21_2015-03-25_03.xml <- The node name
P:\poolman\LY21\2015\LY21_2015-03-25_03.xml <- The dictionary first key
But keep crashing. How can I get rid of this?
I've added
Console.WriteLine(node.Name.Length + " " + Tests.First().Key.Length);
Which output
43 43
So it's not about the length.
In the Immediate window I tried this:
node.Name == Tests.First().Key
true
I really don't understand.
And one more lovely edit:
Tests.ContainsKey(node.Name)
true

Categories

Resources