0
        [Serializable]
        public class ComplexArray
        {
            #region Attributes

            /// <summary>
            /// Array Size
            /// </summary>
            protected int m_iSize;

            /// <summary>
            /// Real part of the data
            /// </summary>
            protected double[] m_dReal;

            /// <summary>
            /// Imaginary part of the data
            /// </summary>
            protected double[] m_dImag;

            #region Construction

            /// <summary>
            /// Default constructor
            /// </summary>
            public ComplexArray()
            {
            }


            public override bool Equals(object o)
            {
                if (this == (ComplexArray)o)
                    return true;
                else
                    return false;
            }



            public static bool operator ==(ComplexArray src1, ComplexArray src2)
            {
                if (src1.m_iSize != src2.m_iSize)
                    return false;

                for (int ii = 0; ii < src1.m_iSize; ii++)
                {
                    if (src1.Real[ii] != src2.Real[ii])
                        return false;
                    if (src1.Imag[ii] != src2.Imag[ii])
                        return false;
                }

                return true;
            }

            public static bool operator !=(ComplexArray src1, ComplexArray src2)
            {

                if (src1 == src2)
                    return false;
                else
                    return true;
            }

    }

I have a created a class called complex array and intention of this class is to save real and imaginary numbers and different operators have been overloaded like +,*,!=,==

Assume some function returns the instance of this class.

ComplexArray array = GetValue();

I want to check whether the reference is valid or not...

    if(array != null)
    {
      //proceed further....
    }

Issue : When the value is checked against null value, the exception occurs, because internally != overloaded function calls the ==.

How to avoid this kind of situation in operator overloading? Or how to make the operator != or == to check for the null value and return the correct values(true or false)

Raghav55
  • 3,055
  • 6
  • 28
  • 38
  • 4
    May this will not help, but I would use class/struct for complex number and regular arrays to avoid those problems – Anton May 23 '12 at 07:43
  • 1
    How about `object.Equals(array, null);` or `array.Equals(null);` – Nikhil Agrawal May 23 '12 at 07:44
  • 1
    I agree with [Anton](http://stackoverflow.com/users/149851/anton), there are a bunch of [Collections in C#](http://msdn.microsoft.com/en-us/library/system.collections.generic.aspx) that can be used for this. – default May 23 '12 at 07:52
  • 1
    Why don't you just first check whether *src1* and *src2* are null? – McGarnagle May 23 '12 at 08:00

3 Answers3

0

If you can target .NET Framework 4.0, System.Numerics.Complex looks like what you need.

Alex
  • 23,004
  • 4
  • 39
  • 73
0

Inside the == and != overloaded operators (at the very top) put the following:

if (src1 == null || src2 == null)
    return false;

By the way, I find your complex number implementation is kind of poor. You could use some ready example or at least read it through.

AgentFire
  • 8,944
  • 8
  • 43
  • 90
0

This can be done by this code:

public override bool Equals(object obj)
{
    var other = obj as ComplexArray;

    if(ReferenceEquals(other, null))
    {
        return false;
    }

    if(ReferenceEquals(this, other)
    {
        return true;
    }

    // ToDo: Do all other comparision beyond reference comparision.
}

Also within the == operator you have to check both sides:

public static bool operator ==(ComplexArray src1, ComplexArray src2)
{
    if(ReferenceEquals(src1, null)
       || ReferenceEquals(src2, null))
    {
        return ReferenceEquals(src1, src2);
    }

    return src1.Equals(src2);
}

public static bool operator !=(ComplexArray src1, ComplexArray src2)
{
    return !(src1 == src2);
}

And don't forget, if you override Equals() you have also to override GetHashCode(). A good pattern for doing can also be found on SO.

Community
  • 1
  • 1
Oliver
  • 43,366
  • 8
  • 94
  • 151