user717186
user717186

Reputation:

C# class definition HELP

This is a project question that i just cant seem to answer

using System;

namespace ConsoleApplication2
{
    internal class Equipment : IComparable
    {
        private readonly string type;
        private readonly int serialNo;
        private string colour;
        public decimal cost;

        public Equipment(string type, int serialNo)
        {
            this.type = type == null ? "" : type.Trim();
            this.serialNo = serialNo;
        }

        public string Key
        {
            get { return type + ":" + serialNo; }
        }

        int IComparable.CompareTo(object obj)
        {
            return 0;
        }
    }
}

(a) Override the appropriate method o ensure that different instances of the class that represent the same equipment item will be considered the same in the system.

(b) Override the appropriate method to enable instances of this class to be stored (and found) by key in a hash table

Upvotes: 0

Views: 173

Answers (3)

Konrad Morawski
Konrad Morawski

Reputation: 8394

Writing GetHashCode manually is not that easy. Anyhow, that's code generated for this purpose by ReSharper. It's a complete solution. (It should be contained within your class definition of course). But what would you say, if you were asked - why and how it works? It might be embarassing.

So, apart from GetHashCode and Equals, which others have suggested you reading about, you might also look up http://msdn.microsoft.com/en-us/library/system.object.referenceequals.aspx as well as http://msdn.microsoft.com/en-us/library/a569z7k8(v=VS.100).aspx

As for the mystery behind 397 in GetHashCode, have a look at this question here on StackOverflow: Why is '397' used for ReSharper GetHashCode override?

        public bool Equals(Equipment other)
        {
            if (ReferenceEquals(null, other))
            {
                return false;
            }
            if (ReferenceEquals(this, other))
            {
                return true;
            }
            return Equals(other.colour, colour) && other.cost == cost && other.serialNo == serialNo && Equals(other.type, type);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj))
            {
                return false;
            }
            if (ReferenceEquals(this, obj))
            {
                return true;
            }
            if (obj.GetType() != typeof (Equipment))
            {
                return false;
            }
            return Equals((Equipment) obj);
        }

// note: if "Override the appropriate method to enable instances of this class
// to be stored (and found) by key in a hash table" is supposed to mean that only type and
// serialNo should be taken into account (since they are used to generate
// the Key value) - just remove the lines with cost and colour
        public override int GetHashCode()
        {
            unchecked
            {
                int result = (colour != null ? colour.GetHashCode() : 0);
                result = (result*397) ^ cost.GetHashCode();
                result = (result*397) ^ serialNo;
                result = (result*397) ^ (type != null ? type.GetHashCode() : 0);
                return result;
            }
        }

Upvotes: 0

sll
sll

Reputation: 62504

  1. Override Equals() with an appropriate logic of comparision
  2. Override GetHashCode(), see GetHashCode Guidelines in C#

You must start reading this before doing such a task Why is it important to override GetHashCode when Equals method is overriden in C#?

Upvotes: 1

platon
platon

Reputation: 5340

You should override the Equals and GetHashCode methods for this purpose.

Upvotes: 3

Related Questions