473,413 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

can't make a custom struct = null

I would like to be able to set my struct equal to null in a way similar to
this example:

struct TestStruct {
public int x;
}

....[other stuff]...

public void Main() {
TestStruct testvar;
testvar.x = 4;
if(testvar.x == 4) {
testvar = null;
}
...[more code]...
}

is this possible using operators? I was thinking something like this:

struct TestStruct {
public int x;

public static bool operator null(TestStruct t) {
return (t.x == 0);
}
}

that struct won't compile, but i thought it might help you to understand my
problem, and how I think it might be solved.
I know that this would be easy to do by simply using a class instead of a
struct, but I would like to be able to use other operators as well. Any
help is appreciated. Thanks,

Chris LaJoie
Nov 15 '05 #1
3 6656
On Tue, 12 Aug 2003 12:01:34 -0500, "Michael Mayer"
<mr*****@charter.net> wrote:
I hate to say this, but there was a post on this newsgroup were
somebody overloaded a few operators and made a dummy class. The
message had subject "C# assign null to a struct" sent on Aug-1-2003


Thanks for the note. Just saw it... really quite clever!

The poster defined a dummy class and an implicit conversion from this
dummy class to the struct; then every null assignment would get mapped
to that implicit dummy conversion operator. Same for equality.

But apart from the confusion this would cause to the reader, you
cannot ever define an implicit conversion or comparison to another
reference type because that would cause an ambiguity error since the
null constant itself is typeless (or rather, fits any reference type).
--
http://www.kynosarges.de
Nov 15 '05 #2
On Tue, 12 Aug 2003 09:47:38 +0100, "Simon Trew" <ten.egnaro@werts>
wrote:
That's not strictly true; null is a literal of reference type and it can be
assigned to any variable of reference type; a reference type that has a null
value has a null value of that type.You can't assign a null value of one
type to that of another:


Correct, but the null constant itself matches any reference type.
The compiler cannot infer a specific type from the literal "null",
except by examining the type on the other side of the assignment or
equation (as in the post referenced by Michael Mayer).
--
http://www.kynosarges.de
Nov 15 '05 #3
"Christoph Nahr" <ch************@kynosarges.de> wrote in message
news:gi********************************@4ax.com...
Oh, another thing. Does the dummy class have to public for the
implicit operators to work? I'm not sure about this but if it does,
that would be another reason not to use this method.


Yeah, it seems like it does. I just tried moving Dummy intio
MyStruct, and made it private. I got a compiler error:

error CS0057: Inconsistent accessibility: parameter type
'Namespace1.tInt.Dummy' is less accessible than operator
'Namespace1.tInt.operator ==(Namespace1.tInt, Namespace1.tInt.Dummy)'
Nov 15 '05 #4

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

Similar topics

3
by: sathyashrayan | last post by:
The standard confirms that the following initialization of a struct struct node { --- --- } struct node var = {NULL};
12
by: Franz | last post by:
Greetings, I have created the following types: typedef struct _tbl_certificate{ int cert_id; char *cert_name; }tbl_certificate; typedef struct _cameraObj{ tbl_camera camera;
6
by: sathyashrayan | last post by:
#include<stdio.h> #include<stdlib.h> #include<string.h> struct tree { int data; struct tree *left,*right; }; void init(struct tree *node)
7
by: Steffen Loringer | last post by:
Hi all, I'm using a linked list (double). The program is growing(windows xp task manager) if the showAllListNodes function is activated. But I can't figure out why. Any ideas??? Thanks...
3
by: Karl M | last post by:
Hi everyone, I just notice some strange behaviors on the MS C++ compiler regarding struct default constructor, see the example bellow: struct MyStruct { int a; }; class MyClass { public:
7
by: CaptainBly | last post by:
Okay I've been spending lots of time on this and it's giving me a migraine so I go humble and ask the guru's here. I have several structs typedef struct vert { int x,y,z; struct vert *...
5
by: Mahendra Kumar Kutare | last post by:
I am trying to implement a webserver with boss-worker model thread pool implementation - I have a header declaration threadpool.h as - typedef struct threadpool_work { void (*routine) ();...
3
by: lye85 | last post by:
#include <stdio.h> #include <stdlib.h> #include <string.h> struct account { char *AccName; int Age; double AccBalance; struct account *Next;
1
by: lye85 | last post by:
#include <stdio.h> #include <stdlib.h> #include <string.h> struct account { char AccName; int Age; double AccBalance; struct account *Next;
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.