This code works. I'm not sure it should. It's a standard overwriting of the Equals method. The part that puzzles me is the field 'unitId'. It's a *private* field.
That 'this.unitId' works is no mystery. It's part of the same object. But why doesn't the 'otherTUI.unitId' give a compilation error, let alone work ? It belongs to another (hence 'otherTUI) instantiated object and it is 'private' ? Should it not be out of scope ?
Does the act of casting make it come into scope '(TransportUnitInfo) obj'?
public class TransportUnitInfo
{
private int unitId;
public override bool Equals (object obj)
{
/*
if (obj == null)
return false;
if (!this.GetType().Equals(obj.GetType()))
return false;
*/
TransportUnitInfo otherTUI = (TransportUnitInfo) obj;
return this.unitId == otherTUI.unitId;
}
}
Commented out boilerplate, to not let it distract, it needs to be there but thats not what I'm wondering about.
Loading
VulpesPosted Feb 23, 2012, 2:59 PM
I have a copy of the original ECMA specification for C# and it says in there in section 10.5.2:
"The accessibility domain of a private member includes only the program text of the type in which the member is declared".
I guess that this could be construed to include the situation that we've been discussing.
pagefault pagefaultPosted Feb 23, 2012, 2:03 PM
I was further hesitant due to two reasons.
1) I'm taking a course and we were instructed to make a gettable property of this 'unitId'.
public int UnitId
{
get
{
return unitId;
}
}
and I thought maybe we were supposed to use that.
2) I'm doing this Microsoft heavy course entirely on a LINUX platform lol. And maybe 'Monodevelop' was making a mistake. But 'Monodevelop' in my experience and testimony I read is very good in compliance.
VulpesPosted Feb 23, 2012, 12:53 PM