Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Sendatsu_Yoshimitsu · Aug 04, 2017 at 01:06 PM · booleandictionarystructcomparison

Why isn't my operator override working on structs?

I'm trying to make a custom container to use as a dict key, so I can index values by an int and string instead of by one key. The intended usage is pretty simple:

 public class CalendarKey
 {
     public int myDayOfYear;
     public TimeOfDay myTimeOfDay;
 }
 
     public void TestStructDict()
     {
         CalendarKey keyA = new CalendarKey();
         keyA.myDayOfYear = 1;
         keyA.myTimeOfDay = TimeOfDay.Afternoon;
 
         CalendarKey keyB = new CalendarKey();
         keyB.myDayOfYear = 1;
         keyB.myTimeOfDay = TimeOfDay.Afternoon;
 
         Dictionary<CalendarKey, string> testDict = new Dictionary<CalendarKey, string>();
         testDict.Add(keyA, "Key A");
         Debug.Log(testDict[keyA]);
         Debug.Log(testDict[keyB]);
     }

My expectation is for testDict[keyA] and testDict[keyB] to return the same data, since structs are value types, not reference types, but testDict[keyB] returns a key not found exception. I thought this might mean that I need to manually add a comparison operator:

 public class CalendarKey
 {
     public int myDayOfYear;
     public TimeOfDay myTimeOfDay;
 
     public override bool Equals(object obj)
     {
         if (obj == null)
         {
             return false;
         }
 
         CalendarKey key = (CalendarKey)obj;
         if (key == null)
         {
             return false;
         }
 
         return myDayOfYear == key.myDayOfYear && myTimeOfDay == key.myTimeOfDay;
     }
 }

However, this has no effect, and the error persists- is it simply not possible (or at least recommended) to use structs as dict keys, or am I doing something else wrong here?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rinaruk · Aug 04, 2017 at 01:36 PM 0
Share

I think that's because its comparing its references. Did you try creating dictionary with custom comparer? https://msdn.microsoft.com/en-us/library/ms132072(v=vs.110).aspx https://social.msdn.microsoft.com/Forums/vstudio/en-US/41fd15a3-6fda-4855-afe5-73498fd1a067/example-of-dictionary-collection-with-custom-class-as-key?forum=netfxbcl

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by cgarossi · Aug 04, 2017 at 02:10 PM

You are overriding the method Equals() not the actual = operator.

Use

  return myDayOfYear.Equals(key.myDayOfYear) && myTimeOfDay.Equals(key.myTimeOfDay);

Here's some info on actual operator overloading

https://msdn.microsoft.com/en-us/library/aa288467(v=vs.71).aspx

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sendatsu_Yoshimitsu · Aug 04, 2017 at 02:10 PM 0
Share

Ooh that's really helpful, thank you for clarifying! :)

avatar image cgarossi Sendatsu_Yoshimitsu · Aug 04, 2017 at 02:16 PM 0
Share

You're welcome.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

68 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Comparing string to a dictionary entry? 0 Answers

importing english dictionary as text file? 1 Answer

String comparison, can't use false result as string. 4 Answers

Should I use == to compare boolean in an if statement? 1 Answer

Unity enum map - how to make it more efficient/clean? Linear line complexity. Best practices. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges