J
John
Ok. I'm not sure whether this is cool or perverted, I need second
opinion![Wink ;) ;)](/styles/default/custom/smilies/wink.gif)
I define two classes as follows:
/********************************************/
public sealed class Dummy {
private Dummy(){}
}
public struct tInt {
private Int32 val;
private Boolean def;
private tInt( Int32 pValue, Boolean pDefined ) {
this.val = pValue;
this.def = pDefined;
}
public static bool operator== (tInt lhs, Dummy rhs) {
return !lhs.def;
}
public static bool operator!= (tInt lhs, Dummy rhs) {
return lhs.def;
}
public static implicit operator tInt(Dummy pIn){
return new tInt( 0, false );
}
public override bool Equals( object pOther ) {
if ( pOther is tInt ) {
tInt wOther = (tInt)pOther;
if ( this.def && wOther.def) {
return this.val == wOther.val;
} else {
throw new Exception("not defined");
}
}
return false;
}
public override int GetHashCode() {
return base.GetHashCode();
}
}
/********************************************/
With these definitions I can now compare this struct to null as if it
were a reference type.
For example this works.
/********************************************/
tInt x = null;
if ( x == null ) {
Console.WriteLine( "x is undefined" );
}
/********************************************/
Wow.
I can now take this struct and make it behave just like an int that
also accepts null as a value.
What do you think? Cool or perverted?
opinion
![Wink ;) ;)](/styles/default/custom/smilies/wink.gif)
I define two classes as follows:
/********************************************/
public sealed class Dummy {
private Dummy(){}
}
public struct tInt {
private Int32 val;
private Boolean def;
private tInt( Int32 pValue, Boolean pDefined ) {
this.val = pValue;
this.def = pDefined;
}
public static bool operator== (tInt lhs, Dummy rhs) {
return !lhs.def;
}
public static bool operator!= (tInt lhs, Dummy rhs) {
return lhs.def;
}
public static implicit operator tInt(Dummy pIn){
return new tInt( 0, false );
}
public override bool Equals( object pOther ) {
if ( pOther is tInt ) {
tInt wOther = (tInt)pOther;
if ( this.def && wOther.def) {
return this.val == wOther.val;
} else {
throw new Exception("not defined");
}
}
return false;
}
public override int GetHashCode() {
return base.GetHashCode();
}
}
/********************************************/
With these definitions I can now compare this struct to null as if it
were a reference type.
For example this works.
/********************************************/
tInt x = null;
if ( x == null ) {
Console.WriteLine( "x is undefined" );
}
/********************************************/
Wow.
I can now take this struct and make it behave just like an int that
also accepts null as a value.
What do you think? Cool or perverted?