c# assign null to a struct

J

John

Ok. I'm not sure whether this is cool or perverted, I need second
opinion ;)
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?
 
M

Michael Mayer

My first thought is what a cool perversion of the language.
Hopefully James Fisher sees this - he was wanting to know how to
assign null to an int. Of course, things like this in a program I had
to work on would drive me crazy - it would have to VERY well
documented so that you didn't get yourself into trouble (not to
mention the work of overloading all the operators needed to make it
behave more like a real Int32)

-mike
 
J

John

Even better, I can use the System.DBNull class instead of the Dummy class.
DBNull.Value could be assigned, and compared to as well as null. It all
seems to be falling into place too easily... Again, wow.
 
J

Jay B. Harlow [MVP - Outlook]

John,
Look at structures in System.Data.SqlTypes for similar implementation.

Jay
 
J

John

I've seen these types, but I don't like having to call a method (or a
property whatever) to find out if it's defined. However I've been thinking
about this some more (its keeping me from sleeping...) and I've realized how
useless these types are in reality :(

static void Main(string[] args) {
tInt x = null;
Console.WriteLine( isNull(x) );
}

public static bool isNull( object x ) {
return x == null;
}

Go figure, this always return false for a struct because of boxing (sob).
Which brings me back to my original thoughts; In a world of virtual
machines and vast amounts of processing power, the added complexity of value
types is just not worth it.
Thanks for the reality check I guess. At least now I can sleep in peace!
 
J

Jay B. Harlow [MVP - Outlook]

John,
I've realized how useless these types are in reality :(
Maybe not:

Consider:
public static bool isNull( object x ) {
if {x is INullable}
((INullable)x).IsNull;
else
return x == null;
}

Basically if its a type that supports the INullable interface, ask the
object if its null, otherwise check to see if a null was passed.

The INullable interface is defined in the System.Data.SqlTypes namespace.
The interface is implemented by both the Sql data types & the Oracle data
types.

Hope this helps
Jay

John said:
I've seen these types, but I don't like having to call a method (or a
property whatever) to find out if it's defined. However I've been thinking
about this some more (its keeping me from sleeping...) and I've realized how
useless these types are in reality :(

static void Main(string[] args) {
tInt x = null;
Console.WriteLine( isNull(x) );
}

public static bool isNull( object x ) {
return x == null;
}

Go figure, this always return false for a struct because of boxing (sob).
Which brings me back to my original thoughts; In a world of virtual
machines and vast amounts of processing power, the added complexity of value
types is just not worth it.
Thanks for the reality check I guess. At least now I can sleep in peace!

John,
Look at structures in System.Data.SqlTypes for similar implementation.

Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top