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 6663773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:32:10+00:00 2026-05-26T02:32:10+00:00

Is it the best override for Equals (in VB.NET) for an object having an

  • 0

Is it the best override for Equals (in VB.NET) for an object having an unique ID?

  Public Overrides Function Equals(ByVal obj As Object) As Boolean
    If obj Is Nothing OrElse Not [GetType]().Equals(obj.GetType()) Then
      Return False
    End If
    Dim otherMyObject As MyObject = DirectCast(obj, MyObject)
    Return Me.Id = otherMyObject.Id
  End Function

I took that example from the MSDN, but not entirely sure if from all points of view (including performance) is the better solution.

EDIT:

How about comparing with this version:

Public Overrides Function Equals(ByVal obj As Object) As Boolean
  Dim otherMyObject As MyObject = TryCast(obj, MyObject)
  If otherMyObject Is Nothing Then
    Return False
  Else
    Return Me.Id = otherMyObject.Id
  End If
End Function
  • 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-26T02:32:10+00:00Added an answer on May 26, 2026 at 2:32 am

    It depends what you mean by “an unique ID”. If you can guarantee that there won’t be any other objects in memory with the same ID, you can just inherit the functionality from Object. If, however, you want to treat two objects of the same type and ID as being equal, then this looks fine. You’d want to override GetHashCode as well though.

    You really need to consider what is going to use the equality implementation and what they’re expecting. If there can be two distinct objects with the same ID but different other properties, what does it mean for those to be “equal”? Is that appropriate or not?

    It may make more sense to implement IEqualityComparer(Of T) in something like an “IdEqualityComparer” type, for example. That’s a good way of expressing that an equality relation is useful but not necessarily a “natural, comprehensive” equality relation.

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

Sidebar

Related Questions

What would be the best way to override the GetHashCode function for the case,
My work code is @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { Toast tst;
Best method to add/remove webcontrols or user controls in asp.net for dynamic forms without
Best to describe this in code... I have this public class A<T> { public
Best practice is to use unique ivs, but what is unique? Is it unique
Best explained with code I think, this is just a simple example: public class
Best Guess: method - def(self, maybeSomeVariables); lines of code which achieve some purpose Function
What is the best way to override jre_lib classes? Let's say I need to
Is it possible to override a function based on the number of parameters that
What's the best way to override validation messages for login/password in the Session model

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.