473,605 Members | 2,637 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

c# assign null to a struct

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.GetHashCod e();
}
}
/*************** *************** **************/

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.WriteLi ne( "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?
Nov 15 '05 #1
10 11473
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

"John" <fu******@hotma il.com> wrote in message
news:f3******** *************** ***@posting.goo gle.com...
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.GetHashCod e();
}
}
/*************** *************** **************/

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.WriteLi ne( "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?

Nov 15 '05 #2
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

"John" <fu******@hotma il.com> wrote in message
news:f3******** *************** ***@posting.goo gle.com...
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.GetHashCod e();
}
}
/*************** *************** **************/

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.WriteLi ne( "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?

Nov 15 '05 #3
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.

"Michael Mayer" <mr*****@charte r.net> wrote in message
news:#t******** ******@tk2msftn gp13.phx.gbl...
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

"John" <fu******@hotma il.com> wrote in message
news:f3******** *************** ***@posting.goo gle.com...
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.GetHashCod e();
}
}
/*************** *************** **************/

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.WriteLi ne( "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?


Nov 15 '05 #4
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.

"Michael Mayer" <mr*****@charte r.net> wrote in message
news:#t******** ******@tk2msftn gp13.phx.gbl...
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

"John" <fu******@hotma il.com> wrote in message
news:f3******** *************** ***@posting.goo gle.com...
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.GetHashCod e();
}
}
/*************** *************** **************/

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.WriteLi ne( "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?


Nov 15 '05 #5
John,
Look at structures in System.Data.Sql Types for similar implementation.

Jay
"John" <fu******@hotma il.com> wrote in message
news:f3******** *************** ***@posting.goo gle.com...
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.GetHashCod e();
}
}
/*************** *************** **************/

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.WriteLi ne( "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?

Nov 15 '05 #6
John,
Look at structures in System.Data.Sql Types for similar implementation.

Jay
"John" <fu******@hotma il.com> wrote in message
news:f3******** *************** ***@posting.goo gle.com...
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.GetHashCod e();
}
}
/*************** *************** **************/

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.WriteLi ne( "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?

Nov 15 '05 #7
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.WriteLi ne( 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!

"Jay B. Harlow [MVP - Outlook]" <Ja********@ema il.msn.com> wrote in message
news:ez******** ******@TK2MSFTN GP12.phx.gbl...
John,
Look at structures in System.Data.Sql Types for similar implementation.

Jay
"John" <fu******@hotma il.com> wrote in message
news:f3******** *************** ***@posting.goo gle.com...
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.GetHashCod e();
}
}
/*************** *************** **************/

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.WriteLi ne( "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?


Nov 15 '05 #8
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.WriteLi ne( 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!

"Jay B. Harlow [MVP - Outlook]" <Ja********@ema il.msn.com> wrote in message
news:ez******** ******@TK2MSFTN GP12.phx.gbl...
John,
Look at structures in System.Data.Sql Types for similar implementation.

Jay
"John" <fu******@hotma il.com> wrote in message
news:f3******** *************** ***@posting.goo gle.com...
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.GetHashCod e();
}
}
/*************** *************** **************/

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.WriteLi ne( "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?


Nov 15 '05 #9
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.Sql Types namespace.
The interface is implemented by both the Sql data types & the Oracle data
types.

Hope this helps
Jay

"John" <fu******@hotma il.com> wrote in message
news:X9******** ************@ne ws20.bellglobal .com... 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.WriteLi ne( 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!

"Jay B. Harlow [MVP - Outlook]" <Ja********@ema il.msn.com> wrote in message news:ez******** ******@TK2MSFTN GP12.phx.gbl...
John,
Look at structures in System.Data.Sql Types for similar implementation.

Jay
"John" <fu******@hotma il.com> wrote in message
news:f3******** *************** ***@posting.goo gle.com...
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.GetHashCod e();
}
}
/*************** *************** **************/

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.WriteLi ne( "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?



Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
4567
by: ineedyourluvin1 | last post by:
Hi, I would appreciate if someone could tell me what I'm doing wrong ? #include<iostream> using namepace std ; struct person{ char *firstname ; int age ;
3
7644
by: sathyashrayan | last post by:
The standard confirms that the following initialization of a struct struct node { --- --- } struct node var = {NULL};
0
610
by: John | last post by:
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 {
6
2289
by: Joanna Carter \(TeamB\) | last post by:
Hi folks I have a Generic Value Type and I want to detect when the internal value changes. /////////////////////////////// public delegate void ValueTypeValidationHandler<T>(T oldValue, T newValue); public struct TestType<T> {
3
5617
by: Ñ©ÔÆÓ¥ | last post by:
Hi,all I have a trouble about struct variable,the detail is : I define a new struct which name ServiceProperty,then I declare a variable like this: ServiceProperty instService = null; but compile throw error: error CS0037: ÎÞ·¨½« NULL ת»»³É
26
7060
by: Brett | last post by:
I have created a structure with five fields. I then create an array of this type of structure and place the structure into an array element. Say index one. I want to assign a value to field3 of the structure inside the array. When I try this, an error about late assignment appears. Is it possible to assign a value to a structure field that is in an array? I'm currently getting around the problem by creating a new structure, assign...
11
3767
by: skumar434 | last post by:
Hi everybody, I am faceing problem while assigning the memory dynamically to a array of structures . Suppose I have a structure typedef struct hom_id{ int32_t nod_de; int32_t hom_id;
44
25030
by: sam_cit | last post by:
Hi Everyone, I tried the following program unit in Microsoft Visual c++ 6.0 and the program caused unexpected behavior, #include <stdio.h> #include <string.h> int main() {
1
7757
by: Seisouhen | last post by:
Hi all, I am using mmap to obtain some space(mapped anonymously) and am giving the address of the assigned space to a struct pointer. Then I want to access a member of the struct that the pointer points to. The code is: //----begin struct foo{ int test; }foo; struct foo one,*one_ptr; one_ptr=mmap(NULL,sizeof(one),PROT_READ|PROT_WRITE,MAP_ANON,-1,0);
0
8004
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7934
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8418
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8071
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5445
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3912
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2438
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1541
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1271
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.