473,379 Members | 1,252 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Implementing IEqualityComparer for a struct?

Hi,

I am trying to implement the IEqualityComparer interface for a struct so I can use it as the Key for a Dictionary.

My struct declaration has:
public struct Ring : IEqualityComparer
{
....
}
and I am trying to implement the Equals and GetHashCode methods.
I thought I could use forms like:
public bool Equals(Ring X, Ring Y)
{...
}
and
public int GetHashCode(Ring X)
{...
}
But they gave errors. From the error message I thought I should try the following:

public bool System.Collections.IEqualityComparer.Equals(Ring X, Ring Y)
{...
}
and
public int System.Collections.IEqualityComparer.GetHashCode(R ing X)
{...
}

But they give "modifier 'public' is not valid for this item.

Thanks for any advice!

Regards,
Raj
May 6 '07 #1
6 8476
>public bool Equals(Ring X, Ring Y)
{...
}
and
public int GetHashCode(Ring X)
{...
}
Since you're implementing the non-generic version of the interface,
the parameter types have to be object rather than Ring.

>But they gave errors. From the error message I thought I should try the following:

public bool System.Collections.IEqualityComparer.Equals(Ring X, Ring Y)
{...
}
and
public int System.Collections.IEqualityComparer.GetHashCode(R ing X)
{...
}

But they give "modifier 'public' is not valid for this item.
You can't specify an access modifier when using explicit
implementation. Remove the public modifier.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
May 7 '07 #2
Hi Raj,

Mattias has pointed out the problem in your code, i.e. you should modify
the parameter type from Ring to object.

The interface IEqualityComparer is not generic. The generic version of this
interface is IEqualityComparer<Tin System.Collections.Generic namespace.

When implementing an interface, we could implement implicitly or
explicitly. An explicitly implemented member cannot be accessed through a
class instance, but only through an instance of the interface. When
implementing an interface implicitly, we add the modifier 'public' on the
implementation method; when implementing an interface explicitly, we
shouldn't add any modifier on the implementation method.

For more information on explicit interface implementation, you may read the
following MSDN document:

'Explicit Interface Implementation (C# Programming Guide)'
http://msdn2.microsoft.com/en-us/lib...57(VS.80).aspx

In fact, intellisense in VS05 provides an option to help you implement an
interface while working in the Code Editor. For more information how to do
this, please read the following document:

'How to: Implement Interface'
http://msdn2.microsoft.com/en-us/lib...1w(VS.80).aspx

At last, because Object class has a method called Equals, you'd better
implement the IEqualityComparer interface explicitly to avoid the potential
confusion.

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

May 7 '07 #3
Linda, Mattias, hi,

Thanks for your help! My apologies for still not quite getting it.

I changed my struct definition to include the specific interface:

public struct Ring : System.Collections.Generic.IEqualityComparer<Ring>
{...}

and tried also being explicit with the method definition:
bool System.Collections.Generic.IEqualityComparer<Ring> .Equals(Object mX,
Object mY)
{...}

then I get the error
"System.Collections.Generic.IEqualityComparer<Ring >.Equals in explict
interface declaration is not a member of interface"

If I remove the "System.Collections.Generic.IEqualityComparer<Ring >" I just
get an error that I did not implement the interface member at all.

Thanks again for your advice!

Regards,
Raj
"Linda Liu [MSFT]" <v-****@online.microsoft.comwrote in message
news:rZ**************@TK2MSFTNGHUB02.phx.gbl...
Hi Raj,

Mattias has pointed out the problem in your code, i.e. you should modify
the parameter type from Ring to object.

The interface IEqualityComparer is not generic. The generic version of
this
interface is IEqualityComparer<Tin System.Collections.Generic namespace.

When implementing an interface, we could implement implicitly or
explicitly. An explicitly implemented member cannot be accessed through a
class instance, but only through an instance of the interface. When
implementing an interface implicitly, we add the modifier 'public' on the
implementation method; when implementing an interface explicitly, we
shouldn't add any modifier on the implementation method.

For more information on explicit interface implementation, you may read
the
following MSDN document:

'Explicit Interface Implementation (C# Programming Guide)'
http://msdn2.microsoft.com/en-us/lib...57(VS.80).aspx

In fact, intellisense in VS05 provides an option to help you implement an
interface while working in the Code Editor. For more information how to do
this, please read the following document:

'How to: Implement Interface'
http://msdn2.microsoft.com/en-us/lib...1w(VS.80).aspx

At last, because Object class has a method called Equals, you'd better
implement the IEqualityComparer interface explicitly to avoid the
potential
confusion.

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.

May 7 '07 #4
Hi Raj,

Thank you for your quick response.

For non-generic interface IEqualityComparer, its methods's parameters are
of type object. For generic interface IEqualityComparer<T>, its methods'
parameters are of type T.

The following is a sample.

struct Ring : IEqualityComparer
{
bool IEqualityComparer.Equals(object x, object y)
{
...
}
int IEqualityComparer.GetHashCode(object obj)
{
...
}
}

struct Ring : IEqualityComparer<Ring>
{
bool IEqualityComparer<Ring>.Equals(Ring x, Ring y)
{
...
}
int IEqualityComparer<Ring>.GetHashCode(Ring obj)
{
...
}
}

In fact, you needn't write the above stubs by yourself when you're using
VS05. Intellisense has provided an option to help you generate the stubs.

To do this, in the code editor, put the input focus within the word
'IEqualityComparer' and click the right mouse button. Choose 'Implement
Interface | Implement Interface Explicitly' from the context menu. Then the
stubs for explicit implementation are generated automatically.

Hope this helps.
If you have anything unclear, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

May 8 '07 #5
Raj,

In addition to the advice given by Mattias and Linda, it should also be
noted that you shouldn't be implementing the IEqualityComparer interface on
the type that you want to compare two instances of. This interface is meant
to be implemented on a type outside of the instances being compared. The
reason for this is in case one or both of the instances is/are null.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Raj Wall" <me********@nospam.nospamwrote in message
news:u0**************@TK2MSFTNGP05.phx.gbl...
Hi,

I am trying to implement the IEqualityComparer interface for a struct so I
can use it as the Key for a Dictionary.

My struct declaration has:
public struct Ring : IEqualityComparer
{
....
}
and I am trying to implement the Equals and GetHashCode methods.
I thought I could use forms like:
public bool Equals(Ring X, Ring Y)
{...
}
and
public int GetHashCode(Ring X)
{...
}
But they gave errors. From the error message I thought I should try the
following:

public bool System.Collections.IEqualityComparer.Equals(Ring X, Ring Y)
{...
}
and
public int System.Collections.IEqualityComparer.GetHashCode(R ing X)
{...
}

But they give "modifier 'public' is not valid for this item.

Thanks for any advice!

Regards,
Raj
May 8 '07 #6
Nicholas, hi,

The reason I was implementing on the type was that I was hoping to snag the
equality comparison request that Dictionary would be making. I'm not sure
where else Dictionary would be looking in order to compare two keys? Thanks
for your help and advice!

Regards,
Raj

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:um**************@TK2MSFTNGP04.phx.gbl...
Raj,

In addition to the advice given by Mattias and Linda, it should also be
noted that you shouldn't be implementing the IEqualityComparer interface
on the type that you want to compare two instances of. This interface is
meant to be implemented on a type outside of the instances being compared.
The reason for this is in case one or both of the instances is/are null.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Raj Wall" <me********@nospam.nospamwrote in message
news:u0**************@TK2MSFTNGP05.phx.gbl...
Hi,

I am trying to implement the IEqualityComparer interface for a struct so I
can use it as the Key for a Dictionary.

My struct declaration has:
public struct Ring : IEqualityComparer
{
...
}
and I am trying to implement the Equals and GetHashCode methods.
I thought I could use forms like:
public bool Equals(Ring X, Ring Y)
{...
}
and
public int GetHashCode(Ring X)
{...
}
But they gave errors. From the error message I thought I should try the
following:

public bool System.Collections.IEqualityComparer.Equals(Ring X, Ring Y)
{...
}
and
public int System.Collections.IEqualityComparer.GetHashCode(R ing X)
{...
}

But they give "modifier 'public' is not valid for this item.

Thanks for any advice!

Regards,
Raj

May 11 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Ben | last post by:
Hi all, I implemented a stack in C++ in 2 different ways and I'd like to know which approach is better than the other.. and if there is any difference between the two? I'd also like to know if...
1
by: Samuel | last post by:
Hi, I used to have this with .NET 1.1: Dim objDict As Hashtable = New Hashtable(New CaseInsensitiveHashCodeProvider(), New CaseInsensitiveComparer()) but when I code the above statement in...
15
by: Bernard | last post by:
Hi All, I am not sure if I should be asking this question on clc or clc++. Let me try on both. I hope that this is not too trivial for the brilliant minds over here. I know that OOP questions...
0
by: Eric Donoho | last post by:
New warning in .net 2.0... The cumbersome, and difficult to remember case-insensitive comparer declaration for the hashtable has been deprecated... It tells me to use IEqualityComparer instead. ...
3
by: dwaach | last post by:
Hi, I am thinking about implementing isInstanceOf() in c. Any ideas ??? Ex. int findType(struct x) { struct X orgX;
3
by: dwaach | last post by:
Hi, I am thinking about implementing isInstanceOf() in c. Any ideas ??? Ex. int findType(struct x)
2
by: psawant | last post by:
Hi all, I have a custom colllelctoin class which inherits collecton base. It is a collecton of objects of type X and X has ID integer as like a unique key. How do i check if the object...
15
by: junky_fellow | last post by:
Hi, Is it possible to implement sizeof as a C function ?
3
by: Daniel Kraft | last post by:
Hi, I usually program in C++, but for a special project I do have to code in C (because it may be ported to embedded-like platforms where no C++ compiler exists). I do want to realize some...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.