Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6656469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:38:50+00:00 2026-05-26T01:38:50+00:00

I have a theoretical class Name_Order, that has a string Name and a int

  • 0

I have a theoretical class Name_Order, that has a string Name and a int Order.

I need to indicate that two Name_Order‘s are different, if the pair NameOrder is different, that is, or name or order are different.

Now, overriding Equals no problemo, but I have some “issues” with GetHashCode:

Public Class Name_Order
  Public Property Name As String
  Public Property Order As Integer

  Public Overrides Function Equals(ByVal obj As Object) As Boolean
    If TypeOf obj Is Name_Order Then
      Dim no = DirectCast(obj, Name_Order)
      Return Me.Name = no.Name AndAlso Me.Order = no.Order
    Else
      Return MyBase.Equals(obj)
    End If
  End Function

  Public Overrides Function GetHashCode() As Integer
    Dim hcName = 0
    If Me.Name IsNot Nothing Then
      hcName = Me.Name.GetHashCode
    End If

    Dim hcOrder = Me.Order.GetHashCode

    Return hcName + hcOrder
  End Function
End Class

In that case, summing the hashcodes, leave a (small, but real) possibility that two distinct Name_Orders with different names or orders be “identical”.

Say, adding the 7 + 154 gives the same result as adding 154 + 7…

An alternative override of that method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:38:51+00:00Added an answer on May 26, 2026 at 1:38 am

    Firstly, while avoiding collisions is good, it isn’t a problem if there are collisions. But a common approach is something like:

    return 7 * x.GetHashCode() + y.GetHashCode();
    

    This is more noticeable in, for example, a point/position, where it would be nice to avoid obvious diagonal collisions like (2,3) vs (3,2).

    A much bigger problem in your code is that the properties in the hash/equals are mutable; if they get changed, any usage in dictionaries etc will stop working. You should prefer read-only key values.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that I need to Unit Test. For background I'm developing
I have the following entity public class DocumentHistory { public string Name { get;
lets say I have the following public class A { private string _someField; public
This is mainly a theoretical question i.e I don't have any need for a
I have my templated container class that looks like this: template< class KeyType, class
I have a logging framework that I've written that has the ability to track
I'm creating a (theoretically) simple hasMany relationship within a domain class. I have two
Let's say I have a theoretical MVC framework that uses a ViewData object to
I have a very theoretical question: Is there a way to ban the use
I have been struggling to find an answer to this theoretical question, even tho

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.