473,756 Members | 2,977 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Implementing IEqualityCompar er for a struct?

Hi,

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

My struct declaration has:
public struct Ring : IEqualityCompar er
{
....
}
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(Rin g X)
{...
}
But they gave errors. From the error message I thought I should try the following:

public bool System.Collecti ons.IEqualityCo mparer.Equals(R ing X, Ring Y)
{...
}
and
public int System.Collecti ons.IEqualityCo mparer.GetHashC ode(Ring X)
{...
}

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

Thanks for any advice!

Regards,
Raj
May 6 '07 #1
6 8512
>public bool Equals(Ring X, Ring Y)
{...
}
and
public int GetHashCode(Rin g 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.Collecti ons.IEqualityCo mparer.Equals(R ing X, Ring Y)
{...
}
and
public int System.Collecti ons.IEqualityCo mparer.GetHashC ode(Ring 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 IEqualityCompar er is not generic. The generic version of this
interface is IEqualityCompar er<Tin System.Collecti ons.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 IEqualityCompar er 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.Collecti ons.Generic.IEq ualityComparer< Ring>
{...}

and tried also being explicit with the method definition:
bool System.Collecti ons.Generic.IEq ualityComparer< Ring>.Equals(Ob ject mX,
Object mY)
{...}

then I get the error
"System.Collect ions.Generic.IE qualityComparer <Ring>.Equals in explict
interface declaration is not a member of interface"

If I remove the "System.Collect ions.Generic.IE qualityComparer <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.mic rosoft.comwrote in message
news:rZ******** ******@TK2MSFTN GHUB02.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 IEqualityCompar er is not generic. The generic version of
this
interface is IEqualityCompar er<Tin System.Collecti ons.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 IEqualityCompar er 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 IEqualityCompar er, its methods's parameters are
of type object. For generic interface IEqualityCompar er<T>, its methods'
parameters are of type T.

The following is a sample.

struct Ring : IEqualityCompar er
{
bool IEqualityCompar er.Equals(objec t x, object y)
{
...
}
int IEqualityCompar er.GetHashCode( object obj)
{
...
}
}

struct Ring : IEqualityCompar er<Ring>
{
bool IEqualityCompar er<Ring>.Equals (Ring x, Ring y)
{
...
}
int IEqualityCompar er<Ring>.GetHas hCode(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
'IEqualityCompa rer' 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 IEqualityCompar er 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.co m

"Raj Wall" <me********@nos pam.nospamwrote in message
news:u0******** ******@TK2MSFTN GP05.phx.gbl...
Hi,

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

My struct declaration has:
public struct Ring : IEqualityCompar er
{
....
}
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(Rin g X)
{...
}
But they gave errors. From the error message I thought I should try the
following:

public bool System.Collecti ons.IEqualityCo mparer.Equals(R ing X, Ring Y)
{...
}
and
public int System.Collecti ons.IEqualityCo mparer.GetHashC ode(Ring 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.c omwrote in
message news:um******** ******@TK2MSFTN GP04.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 IEqualityCompar er 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.co m

"Raj Wall" <me********@nos pam.nospamwrote in message
news:u0******** ******@TK2MSFTN GP05.phx.gbl...
Hi,

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

My struct declaration has:
public struct Ring : IEqualityCompar er
{
...
}
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(Rin g X)
{...
}
But they gave errors. From the error message I thought I should try the
following:

public bool System.Collecti ons.IEqualityCo mparer.Equals(R ing X, Ring Y)
{...
}
and
public int System.Collecti ons.IEqualityCo mparer.GetHashC ode(Ring 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
2740
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 the destructor i'm using is correct.. I got segmentation fault in my second approach while I quit the program. I appreciate any help.... My first appoach goes like this:
1
3948
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 VS.NET 2005, a warning pops up with this message:
15
426
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 have been asked on clc before so it is probably OK. I am a newbie to C++. BS 3rd edition states: % The throw transfers control to a handler for exceptions .... %
0
3889
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. So, this syntax is obsolete: New Hashtable(New CaseInsensitiveHashCodeProvider, New CaseInsensitiveComparer) So, the replacement seems to be (after an hour of searching):
3
2454
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
1427
by: dwaach | last post by:
Hi, I am thinking about implementing isInstanceOf() in c. Any ideas ??? Ex. int findType(struct x)
2
1828
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 already exists in the collection?. I think i am missing something straightfoward.
15
3279
by: junky_fellow | last post by:
Hi, Is it possible to implement sizeof as a C function ?
3
1635
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 kind of polymorphic behaviour, like this: I have some abstract base "class" Base, that is, a struct containing a vtable of function-pointers and possibly some common fields (but at the
0
10040
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9873
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8713
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3359
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.