Reputation: 624
Let me start by saying that I have read up on my problem, specifically this question and this one also. My problem is a little different, however. I understand the differences between the different methods, but cannot get my code to run correctly for the life of me.
In part of my code, I have the comparison below. But the comparison always fails, and "Type is:leg" prints out.
if (String.Compare(timer.Type,"leg",true) == 0)
{
timer.StopTime = DateTime.Now;
// TODO Log into database here
toRemove.Add(timer);
}
//Couple more conditions in here...
else
{
Console.WriteLine("Attempting to remove cycle timer of invalid type");
Console.WriteLine("Type is:" + timer.Type);
//TODO: Log error
}
I also tried alternative methods, but none of them seem to work for me.
if(timer.Type == "leg" || timer.Type == "Leg") //Fails
if(time.Type.Equals("leg") || timer.Type == "Leg") //Fails
String type = timer.Typer; //Worth a shot...
if(type == "leg" || type == "Leg") //No luck
EDIT: More code has been requested, so here is the entire method.
private void stopFinishedTimers(AGVData agv)
{
List<CycleTimer> toRemove = new List<CycleTimer>();
foreach (CycleTimer timer in AllRunningCycleTimers)
{
if (agv.CurrentRFIDNumber == timer.CycleStopRfid)
{
if (String.Compare(timer.Type,"leg",true) == 0)
{
timer.StopTime = DateTime.Now;
// TODO Log into database here
toRemove.Add(timer);
}
else if (timer.Type.Equals("route") || timer.Type.Equals("Route"))
{
timer.StopTime = DateTime.Now;
// TODO Log into database here
toRemove.Add(timer);
}
else
{
Console.WriteLine("Attempting to remove cycle timer of invalid type");
Console.WriteLine("Type is:" + timer.Type);
//TODO: Log error
}
}
}
Where CycleTimers are a class containing a fields called type, accessed through a property.
Upvotes: 1
Views: 134
Reputation: 8446
I'll add my two cents:
The line String type = timer.Typer
seems wrong because the property name is Type
in most of your examples. Also if(time.Type.Equals("leg") || timer.Type == "Leg")
seems suspicious since you are referencing time
and timer
instead of the same variable both times.
Lastly, I always always use StringComparison.Ordinal
when comparing strings in .Net because of culture info and character set differences, etc. I'm not sure if that could be an issue but see this for more information.
EDIT:
On a side note, StringComparison.OrdinalIgnoreCase
is also an option.
Upvotes: 2
Reputation: 271
Debug and put a break statement to see what timer.Type equates to, or put a Console.WriteLine(timer.Type.ToString())... Could timer have gone out of scope?
or maybe change the test to timer.Type.ToString() == "Leg"
Upvotes: 1