Khada Kuraki
Khada Kuraki

Reputation: 123

c# 'string.Equals' returning wrong results?

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? =\

Upvotes: 2

Views: 501

Answers (2)

user47589
user47589

Reputation:

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

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

Upvotes: 3

Yaron
Yaron

Reputation: 2153

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.

Upvotes: 8

Related Questions