Equal values but if return false [Answered]
We have same values, but it remains return false
18 Replies
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.
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 ? 🤨
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
how to do that ?
Object.Equals Method (System)
Determines whether two object instances are equal.
(ignore the inheritance shown, it's a terrible one, but otherwise it shows how to override)
Use that but didn't works, probably I use wrong. But check if any point is equal getting properties and it works!
Thanks guys
show what $code you used for the override
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/ah, gotta drop, have a game starting
Used if(Equals(p1_Start,p2_Start) || Equals(p1_Start,p2_End))
the entire thing
including the method definition
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;
}
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 hereThe 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).Thanks guys, i didn't know about that difference. I'll study about It.
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
✅ This post has been marked as answered!