Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# 'string.Equals' returning wrong results?

Tags:

c#

assert

first time poster so forgive me if my formatting is off or anything :)

I'm working on my game engine in C# using XNA but when I check the name of a new node against existing nodes, the Assert fires off unpredictably even when there is no matching name in the list. Here is the code I'm referring too:

    public void CheckNameIsUnique(string cName)
    {
        for (int i = 0; i < m_aNodeList.Count; ++i)
        {
            Debug.Assert(m_aNodeList[i].GetName().Equals(cName),
                "USE OF NON-UNIQUE NAME: " + cName);
        }
    }

The assert will fire off -for example- when checking, "box1" and the only node in the list has the name "RootNode".

I get the same unpredictable results using: string == string and string.CompareTo(string) > 0

Any ideas? =\

like image 457
Khada Kuraki Avatar asked Aug 09 '26 11:08

Khada Kuraki


2 Answers

Assert is supposed to make sure the condition is TRUE. If it's false the assert will fail. What you want is to assert that it's NOT equal. use != and it should be fine.

like image 112
Yaron Avatar answered Aug 11 '26 04:08

Yaron


The assertion fires if the condition is false. You have your conditional backwards. See here:

http://msdn.microsoft.com/en-us/library/e63efys0.aspx


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!