473,513 Members | 2,406 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Null structs

Hello.

I have some structs in different namespaces/classes/other structs and I
sometime have to check if it contains something or not.

myStruct == null doesn't work.

I've currently done it by creating a IsNull method in my structs:

struct TestStruct {
public int Value;

public bool IsNull() {
return this.Equals(new TestStruct());
}
}

Is this correct? Is there another way?
--
S. Lorétan <http://www.tynril.info/(This link is in french. Sorry.)
Oct 2 '06 #1
2 9682
S. Lorétan a écrit :
I have some structs in different namespaces/classes/other structs and I
sometime have to check if it contains something or not.

myStruct == null doesn't work.
Value types (structs) cannot be null, they are always instanciated. By
the way, you are not checking if your instance contains something, you
are checking if your instance is ... instanciated.
I've currently done it by creating a IsNull method in my structs:

struct TestStruct {
public int Value;

public bool IsNull() {
return this.Equals(new TestStruct());
}
}

Is this correct? Is there another way?
As for this example, new TestStruct() creates an instance of TestStruct
with all its fields initialized with their default type value. For an
int (which is a value type), that is 0.
The default implementation of Equals for a value type uses reflection to
check fields equality. So in this case, IsNull will return true if Value
equals 0. I cannot assert the correctness of this behaviour.

Another way ? If your classes may not be instanciated, use reference
types (classes) instead of value types. In .NET 2.0, you could also use
the Nullable<Tclass.
Or have your value type manage its null state. I would have it this way :

interface INullable {
bool IsNull {
get;
}
}

struct TestStruct: INullable {
private int _Value;
private bool _IsNull;

public TestStruct(int value) {
Value=value;
}

public int Value {
get {
if (_IsNull)
throw new InvalidOperationException();
return _Value;
}
set {
_Value=value;
_IsNull=false;
}
}

public bool IsNull {
get {
return !_IsNull;
}
}
}
Mathieu
Oct 2 '06 #2
You can use the new c#2 sintax for nullable value types.
Forexample if you have structure
struct Foo
{
}

you can declare variable such as

Foo? f = null
or
Foo? f = new Foo();

the you can test
if(f == null)
{
}

or
you can use

if(f.HasValue)
{
}

The actuall value type is exposed via the Value property

f.Value returns the actuall value if the HasValue is true.

you can also use casting

Foo foo = (Foo)f;

Keep in mind that the Value property or casting return copy of the value, so
changing properties won't change the original value.
C# also has one new operator related to nullable value types - the ??
operator that can provide deault value if the nullable value type is null.
If you want to use this new feature of the language I'd suggest reading in
MSDN about it since it has some details related to arithmetic and
comparision operations when one of the operands is null. Just go in the MSDN
search tool and use "nullbale types" as a keyword.
--
HTH
Stoitcho Goutsev (100)
"Mathieu Cartoixa" <mathieu.cartoixa@_NO_hotmail_SPAM_.comwrote in message
news:45**********************@news.free.fr...
S. Lorétan a écrit :
>I have some structs in different namespaces/classes/other structs and I
sometime have to check if it contains something or not.

myStruct == null doesn't work.
Value types (structs) cannot be null, they are always instanciated. By the
way, you are not checking if your instance contains something, you are
checking if your instance is ... instanciated.
>I've currently done it by creating a IsNull method in my structs:

struct TestStruct {
public int Value;

public bool IsNull() {
return this.Equals(new TestStruct());
}
}

Is this correct? Is there another way?
As for this example, new TestStruct() creates an instance of TestStruct
with all its fields initialized with their default type value. For an int
(which is a value type), that is 0.
The default implementation of Equals for a value type uses reflection to
check fields equality. So in this case, IsNull will return true if Value
equals 0. I cannot assert the correctness of this behaviour.

Another way ? If your classes may not be instanciated, use reference types
(classes) instead of value types. In .NET 2.0, you could also use the
Nullable<Tclass.
Or have your value type manage its null state. I would have it this way :

interface INullable {
bool IsNull {
get;
}
}

struct TestStruct: INullable {
private int _Value;
private bool _IsNull;

public TestStruct(int value) {
Value=value;
}

public int Value {
get {
if (_IsNull)
throw new InvalidOperationException();
return _Value;
}
set {
_Value=value;
_IsNull=false;
}
}

public bool IsNull {
get {
return !_IsNull;
}
}
}
Mathieu

Oct 2 '06 #3

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

Similar topics

3
7635
by: sathyashrayan | last post by:
The standard confirms that the following initialization of a struct struct node { --- --- } struct node var = {NULL};
9
2849
by: Steve Sargent | last post by:
Hi: I'm trying to debug the following code, and it keeps looping on the if statement: public static bool operator == (OnlineMemberNode first, OnlineMemberNode second) { if(first == null) {
5
2234
by: Bob Gregory | last post by:
Hi all, I'm utter C# newbie, do be gentle. VS2005 Express refuses point blank to install on my box, so I'm stuck with C# 1.0 unless someone can point me to a C#2.0 compiler elsewhere. I have...
61
3692
by: Marty | last post by:
I am new to C# and to structs so this could be easy or just not possible. I have a struct defined called Branch If I use Branch myBranch = new Branch(i); // everything works If I use Branch...
13
2488
by: Karch | last post by:
I find myself doing things like this all the time: if ( SomeObject != null && SomeObject.AnotherObject != null && SomeObject.AnotherObject.YetAnother != null &&...
51
4161
by: atv | last post by:
Hi, Just to check, if i set a pointer explicitly to NULL, i'm not allowed to dereference it? Why is that, it's not like it's pointing to any garbage right? Why else set it to NULL. I can remember...
76
4574
by: valentin tihomirov | last post by:
As explained in "Using pointers vs. references" http://groups.google.ee/group/borland.public.delphi.objectpascal/browse_thread/thread/683c30f161fc1e9c/ab294c7b02e8faca#ab294c7b02e8faca , the...
8
2277
by: A. Anderson | last post by:
Howdy everyone, I'm experiencing a problem with a program that I'm developing. Take a look at this stack report from GDB - #0 0xb7d782a3 in strlen () from /lib/tls/i686/cmov/libc.so.6 #1 ...
2
4442
by: JennyWren | last post by:
I have an array of structs which I will be going through one-by-one to search for a matching value. This array might have elements added and removed later, so I don't want to have to update a...
0
7264
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,...
0
7166
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
7386
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7534
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4749
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...
0
3226
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1601
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 ...
1
805
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
459
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...

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.