C
C#•3y ago
Joao Matias

Equal values but if return false [Answered]

We have same values, but it remains return false
18 Replies
Saber
Saber•3y ago
reference types are compared by reference not value unless you have overridden the equals operator. Even though they are the same value, they are not the same instance so they are not equal.
Joao Matias
Joao MatiasOP•3y ago
Sure, so the way that we can compare if it's true is getting X,Y and Z of each element and compare it ? Or it will return false ? 🤨
jcotton42
jcotton42•3y ago
For such a simple structure you might as well use a record if you can It will do this equality Otherwise you'll need to override Equals
Joao Matias
Joao MatiasOP•3y ago
how to do that ?
jcotton42
jcotton42•3y ago
(ignore the inheritance shown, it's a terrible one, but otherwise it shows how to override)
Joao Matias
Joao MatiasOP•3y ago
Use that but didn't works, probably I use wrong. But check if any point is equal getting properties and it works! Thanks guys
jcotton42
jcotton42•3y ago
show what $code you used for the override
MODiX
MODiX•3y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
jcotton42
jcotton42•3y ago
ah, gotta drop, have a game starting
Joao Matias
Joao MatiasOP•3y ago
Used if(Equals(p1_Start,p2_Start) || Equals(p1_Start,p2_End))
jcotton42
jcotton42•3y ago
the entire thing including the method definition
Joao Matias
Joao MatiasOP•3y ago
public static List<ElementId> checkPositionElements(Pipe p, List<Pipe> pipes) { LocationCurve pLocation = p.Location as LocationCurve; XYZ p1_Start = pLocation.Curve.Evaluate(0, true); XYZ p1_End = pLocation.Curve.Evaluate(1, true); List<ElementId> returnList = new List<ElementId>(); foreach (Pipe pipe in pipes) { if (pipe == null) continue; if (pipe.Id == p.Id) continue; LocationCurve p2_Location = pipe.Location as LocationCurve; XYZ p2_Start = p2_Location.Curve.Evaluate(0, true); XYZ p2_End = p2_Location.Curve.Evaluate(1, true); if (Equals(p1_Start.X,p2_Start) || Equals(p1_Start, p2_End)) { if (Equals(p1_End, p2_Start) || Equals(p1_End,p2_End)) { returnList.Add(pipe.Id); } } } return returnList; }
Pobiega
Pobiega•3y ago
jcotton42 told you to override what Equals does, not just use equals to do the comparison but yeah, you should probably just use a record instead of class here
Thinker
Thinker•3y ago
The static Equals always compares by reference. You'll have to override and call the instance Equals in order to handle equality (or just use a record, much simpler).
Joao Matias
Joao MatiasOP•3y ago
Thanks guys, i didn't know about that difference. I'll study about It.
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Accord
Accord•3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server