1

I am reading the "Head First Object Oriented Design and Analysis" and I am stuck on page 254.

In the java code below, I am trying to convert the "Matches" method to a c# one.

public class InstrumentSpec {

  private Map properties;

  public InstrumentSpec(Map properties) {
    if (properties == null) {
      this.properties = new HashMap();
    } else {
      this.properties = new HashMap(properties);
    }
  }

  public Object getProperty(String propertyName) {
    return properties.get(propertyName);
  }

  public Map getProperties() {
    return properties;
  }

  public boolean matches(InstrumentSpec otherSpec) {
    for (Iterator i = otherSpec.getProperties().keySet().iterator(); 
         i.hasNext(); ) {
      String propertyName = (String)i.next();
      if (!properties.get(propertyName).equals(
           otherSpec.getProperty(propertyName))) {
        return false;
      }
    }
    return true;
  }
}

And this is the C# code that I have so far:

public class InstrumentSpec
{
    private IDictionary _properties;

    public InstrumentSpec(IDictionary properties)
    {
        this._properties = properties == null ? new Hashtable() : new Hashtable(properties);
    }

    public object GetProperty(string propertyName)
    {
        return _properties.Contains(propertyName);
    }

    public IDictionary Properties
    {
        get { return _properties; }
        set { _properties = value; }
    }

    public virtual bool Matches(InstrumentSpec otherSpec)
    {
        foreach (var prop in otherSpec.Properties)
        {
            if (!prop.Equals(otherSpec.Properties))
            {
                return false;

            }
        }
        return true;
    }
}

Anyone has got any idea how to make the Matching method work so that it checks if two objects match?

2
  • isnt there an .equals() for every object? Commented Sep 1, 2014 at 11:46
  • yes there is an equals() method on every object and as you can see I have used it in my code but I can't seem to get it right with IDictionary. Commented Sep 1, 2014 at 11:50

4 Answers 4

2

The Java code iterates over the dictionary keys and compares the respective property values. You're currently iterating over the key/value pairs and compare them to the dictionary.

I guess something like

foreach (var key in otherSpec.Properties.Keys)
{
  if (!Properties[key].Equals(otherSpec.Properties[key]))
  {
    return false;
  }
}
return true;

would be a better translation.

Sign up to request clarification or add additional context in comments.

Comments

1

Look at your comparison:

if (!prop.Equals(otherSpec.Properties))

When do you expect any single "property" to equal the collection of "properties" which contains it? The Java code is making comparisons with the object's internal collection of "properties":

if (!properties.get(propertyName).equals(
       otherSpec.getProperty(propertyName)))

Which basically means it's looping through a collection of "properties" and for each "property" in that collection it is comparing it with a similarly named "property" in another collection. But you don't make any reference to the object's collection here:

private IDictionary _properties;

You need to compare the values from one collection to the values in the other collection. Without doing any checking if the values actually exist in the collection (which I recommend doing), it might look something like this:

foreach (var prop in otherSpec.Properties.Keys)
{
    if (!otherSpec.Properties[prop].Equals(_properties[prop]))
    {
        return false;
    }
}
return true;

Comments

1

You could completely copy the algorithm, if thats what you want:

public virtual bool Matches(InstrumentSpec otherSpec)
{
    foreach (var prop in otherSpec.Properties.Keys)
    {
        if (!Object.equals(properties[prop], otherSpec[prop]))
        {
            return false;
        }
    }
    return true;
}

But i would advise, to use generics, to know, which types we are talking about

Comments

1

Try this:

  var keysEqual= Properties.Keys.SequenceEqual(otherSpec.Properties.Keys);
    var valuesEqual = Properties.Values.SequenceEqual(otherSpec.Properties.Values);

if(keysEqual && valueEqual)
{
//objects have the same properties and values
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.