Click here to Skip to main content
15,887,135 members
Home / Discussions / C#
   

C#

 
AnswerRe: Problem with override the operators Pin
Eddy Vluggen13-Mar-13 6:37
professionalEddy Vluggen13-Mar-13 6:37 
GeneralRe: Problem with override the operators Pin
VendorX13-Mar-13 6:47
VendorX13-Mar-13 6:47 
GeneralRe: Problem with override the operators Pin
Eddy Vluggen13-Mar-13 7:20
professionalEddy Vluggen13-Mar-13 7:20 
AnswerRe: Problem with override the operators Pin
Dave Kreskowiak13-Mar-13 6:48
mveDave Kreskowiak13-Mar-13 6:48 
AnswerRe: Problem with override the operators Pin
Richard MacCutchan13-Mar-13 7:24
mveRichard MacCutchan13-Mar-13 7:24 
AnswerRe: Problem with override the operators Pin
Pete O'Hanlon13-Mar-13 7:27
mvePete O'Hanlon13-Mar-13 7:27 
GeneralRe: Problem with override the operators Pin
VendorX13-Mar-13 7:49
VendorX13-Mar-13 7:49 
AnswerRe: Problem with override the operators Pin
DaveyM6913-Mar-13 9:02
professionalDaveyM6913-Mar-13 9:02 
A rough untested example of how I do this for a class (it's much simpler for a struct as it can never be null):
C#
// An immutable int wrapper class
public class SomeClass : IEquatable<SomeClass>
{
    private int index;

    public SomeClass(int index)
    {
        this.index = index;
    }

    public static bool operator ==(SomeClass instance, SomeClass other)
    {
        if(object.ReferenceEquals(instance, other))
            return true;
        if(object.ReferenceEquals(null, instance) || object.ReferenceEquals(null, other))
            return false;
        return instance.index == other.index;
    }
    public static bool operator !=(SomeClass instance, SomeClass other)
    {
        return !(instance == other);
    }

    public int Index
    {
        get { return index; }
    }

    public override bool Equals(object obj)
    {
        return Equals(obj as SomeClass);
    }
    public bool Equals(SomeClass other)
    {
        if(object.ReferenceEquals(null, other))
            return false;
        return index.Equals(other.index);
    }
    public override int GetHashCode()
    {
        return index;
    }
}

And a struct:
C#
// An immutable int wrapper struct
public struct SomeStruct : IEquatable<SomeStruct>
{
    private int index;

    public SomeStruct(int index)
    {
        this.index = index;
    }

    public static bool operator ==(SomeStruct instance, SomeStruct other)
    {
        return instance.index == other.index;
    }
    public static bool operator !=(SomeStruct instance, SomeStruct other)
    {
        return !(instance == other);
    }

    public int Index
    {
        get { return index; }
    }

    public override bool Equals(object obj)
    {
        if(obj is SomeStruct)
            return Equals((SomeStruct)obj);
        return false;
    }
    public bool Equals(SomeStruct other)
    {
        return index.Equals(other.index);
    }
    public override int GetHashCode()
    {
        return index;
    }
}

Dave

Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.
Astonish us. Be exceptional. (Pete O'Hanlon)

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)



AnswerRe: Problem with override the operators Pin
DaveyM6913-Mar-13 9:10
professionalDaveyM6913-Mar-13 9:10 
GeneralRe: Problem with override the operators Pin
VendorX13-Mar-13 9:38
VendorX13-Mar-13 9:38 
GeneralRe: Problem with override the operators Pin
MicroVirus13-Mar-13 12:06
MicroVirus13-Mar-13 12:06 
QuestionMdi form in c# Pin
keyur_raval13-Mar-13 2:09
keyur_raval13-Mar-13 2:09 
AnswerRe: Mdi form in c# Pin
Pete O'Hanlon13-Mar-13 2:17
mvePete O'Hanlon13-Mar-13 2:17 
QuestionHow to record the video from a System.Windows.Forms.Panel? Pin
makhondi13-Mar-13 1:33
makhondi13-Mar-13 1:33 
AnswerRe: How to record the video from a System.Windows.Forms.Panel? Pin
NotPolitcallyCorrect13-Mar-13 1:43
NotPolitcallyCorrect13-Mar-13 1:43 
AnswerRe: How to record the video from a System.Windows.Forms.Panel? Pin
makhondi13-Mar-13 1:50
makhondi13-Mar-13 1:50 
GeneralRe: How to record the video from a System.Windows.Forms.Panel? Pin
NotPolitcallyCorrect13-Mar-13 2:36
NotPolitcallyCorrect13-Mar-13 2:36 
AnswerRe: How to record the video from a System.Windows.Forms.Panel? Pin
Pete O'Hanlon13-Mar-13 2:08
mvePete O'Hanlon13-Mar-13 2:08 
AnswerRe: How to record the video from a System.Windows.Forms.Panel? Pin
Dave Kreskowiak13-Mar-13 6:47
mveDave Kreskowiak13-Mar-13 6:47 
QuestionHelp! Create File Pin
makhondi13-Mar-13 0:59
makhondi13-Mar-13 0:59 
AnswerRe: Help! Create File Pin
Garth J Lancaster13-Mar-13 1:12
professionalGarth J Lancaster13-Mar-13 1:12 
AnswerRe: Help! Create File Pin
Pete O'Hanlon13-Mar-13 1:13
mvePete O'Hanlon13-Mar-13 1:13 
AnswerRe: Help! Create File Pin
Garth J Lancaster13-Mar-13 1:13
professionalGarth J Lancaster13-Mar-13 1:13 
GeneralRe: Help! Create File Pin
GuyThiebaut13-Mar-13 1:48
professionalGuyThiebaut13-Mar-13 1:48 
QuestionMultiple Serial Ports over TCPIP Pin
ritmas12-Mar-13 23:27
ritmas12-Mar-13 23:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.