简体   繁体   中英

Why does this compare answer never change?

I'm writing code that reads a CSV into an array. The 7th item in the CSV is TenNinetyNine or W2 so I compare array[6] and TenNinetyNine and it always gives me 1 back saying they match. I pull up a messagebox with array[6] and it shows that they don't match.

I have tried: if (array[6] == "TenNinetyNine") and using string.Compare which is currently up.

I messed with the CSV which was originally 1099 or W2 just to verify they both read in as strings. All my testing shows that every line I see a new value for array[6] but it seems to keep the first result when they are compared.

string data = sr.ReadLine();
while (data != null)
{
    string[] developerData = data.Split(',');
    string tax1 = "TenNinetyNine";
    int taxCompared = string.Compare(tax1, developerData[6]);

    MessageBox.Show(developerData[6]); //Changes each iteration
    MessageBox.Show(taxCompared.ToString()); //Always 1
}

I expect the MessageBox.Show(taxCompared.ToString()); to produce a 1 for TenNinetyNine and a -1 (or 0) for W2 .

Put a breakpoint on the first messagebox so that you can hove over and see tax1 and developerData[6] are identical as I have no idea what developerData[6] is returning. String.Compare is case sensitive so check for that too, ensure there are no leading or trailling spaces by trimming each string:

int taxCompared = string.Compare(tax1.Trim(), developerData[6].Trim();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM