system.stackoverflowexception Cannot evaluate expression because the current thread is in a stack overflow state

StackOverflow https://stackoverflow.com/questions/18189123

Question

I am getting a System.StackOverFlowException when the code hits this function.

Where stringtype is user defined tupe and equals int the function in the type library.

  public static bool Equals(StringType leftHand, StringType rightHand)
  {
       if (leftHand == rightHand)
       {
          return true;
       }
       if ((leftHand == "0") || (rightHand == "0"))
       {
          return false;
       }
       return (leftHand.myValue.Equals(rightHand.myValue) && leftHand.myState.Equals(rightHand.myState));
   }
Was it helpful?

Solution

This

if (leftHand == rightHand)

change to

if (object.ReferenceEquals(leftHand, rightHand))

You probably redefined the == operator to call Equals.

And I hope you don't have an implicit operator that from string creates StringType, because otherwise

if ((leftHand == "0") || (rightHand == "0"))

will probably call itself for the same reason.

Probably

if ((leftHand.myValue == "0") || (rightHand.myValue == "0"))

would be better.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top