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

  • Home
  • SEARCH
  • 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 4081356
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:03:00+00:00 2026-05-20T18:03:00+00:00

If I implement a relationship Car <-> Owner in Delphi using a TDictionary, how

  • 0

If I implement a relationship Car <-> Owner in Delphi using a TDictionary, how should I implement the Equals and GetHashCode function of the IEqualityComparer? (GetHashCode returns an Integer which is used for hashing in TDictionary.)

For the TVehicle class, assume that it has a VIN (vehicle identification number).

How should I implement the hashcode for the VIN?

Update: in this example, object identity does not mean ‘identity of the memory locations of two object pointers’, but ‘identity of two instances of the same object, based on a unique and invariable (“immutable“) combination of its properties’.

So instead of searching a vehicle by its memory address in the map, I need the vehicle which has the id I am looking for.

Think of a database which contains the vehicle-owner data, loaded into the dictionary at application startup. Now if the user enters a VIN in an application form, how can the application find the vehicle in the dictionary? If the code creates a new instance using VehicleFactory.CreateVehicleFromDatabase(Edit1.Text); and search for this object in the dictionary, the default implementation of Equals will not find any entries in the map, because it looks for the memory address. To find the vehicle, Equals needs to compare the VIN.

So I have to create a custom IEqualityComparer. Implementing Equals is trivial. But what about GetHashCode? For a string property, I can not simply use the address of the string (see Berry Kelly in Are Delphi strings immutable? : “If you create the same string from two separate sections of code, they will not share the same backing store”), so a GetHashCode function for a string property needs a customized implementation.

I also found I found the question How do I hash a string with Delphi? – there is an example which contains a HashValue('Hello World')

  • 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-20T18:03:00+00:00Added an answer on May 20, 2026 at 6:03 pm

    You appear to have been misled into the belief that Delphi strings do not come with a default hash code implementation.

    This is not the case. When you create a TDictionary with a string value as a key, the hash is calculated based on the contents of the string. If Value is a string variable then the code looks like this:

    BobJenkinsHash(Value[1], Length(Value) * SizeOf(Value[1]), 0);
    

    I think this answers the part of your question concerning string hashing.


    The comments to the other answers, and the ones I have deleted were an interesting discussion on the design problem you are contemplating. I’m still sceptical of your belief that the right solution is to allow a many-to-one relationship between TVehicle instances and VIN.

    You have confirmed that you must not have multiple TVehicle instances with the same VIN but differing data. It seems to me that the best way to achieve this is to ensure that you have a one-to-one relationship between TVehicle instances and VIN.

    This one-to-one relationship is quite easy to achieve. You need to make the instantiation of TVehicle instances a function private to a factory class. This factory class holds a dictionary containing the existing vehicle instances, TDictionary<string,TVehicle>. If you need to get hold of a vehicle you ask the factory for it. It returns either an existing one that was located in its dictionary, or synthesises a new one.

    There are no doubt a number of other ways to achieve this effect, but I would strongly urge you to consider an approach that results in only a single vehicle instance per VIN.

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

Sidebar

Related Questions

No related questions found

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.