A structs IsEmpty method.. how do you implement one?

  • Thread starter Thread starter wyrd
  • Start date Start date
W

wyrd

Structures in the .NET framework have an IsEmpty method
(Rectangle.IsEmpty for example). Does anyone know how these methods
are implemented?

Thanks in advance.
 
Wyrd,
I would check each member of the structure for default values. (0, null,
nothing).

As those are the values the structure will have if you do not initialize the
structure in VB.NET. C# you normally get warnings if you do not initialize
the structure...

Also remember if you implement IsEmpty you should consider implementing a
shared/static readonly Empty field also. (Actually its not a bad idea to
implement a Empty field even without an IsEmpty method).

Hope this helps
Jay
 
"..."
Also remember if you implement IsEmpty you should consider
implementing a shared/static readonly Empty field also. (Actually its
not a bad idea to implement a Empty field even without an IsEmpty
method).

Why would you want to make that field shared / static? Shouldn't it be per
instance?

Greetz,
-- Rob.
 
Rob Tillie said:
"..."

Why would you want to make that field shared / static? Shouldn't it be per
instance?

No, the idea is that Empty *returns* an instance, namely one where
IsEmpty returns true.
 
wyrd,

IsEmpty is not a method but a read-only property. In the Rectangle class
case, this property return the boolean true if Width, Height, X, and Y are
all uqual to 0, false otherwise.

Gabriele
 
wyrd,

IsEmpty is not a method but a read-only property. In the Rectangle class
case, this property return the boolean true if Width, Height, X, and Y are
all equal to 0, false otherwise.

Gabriele
 
Rob,
As Jon stated, the Empty field is a 'blank' instance of the Structure in
question. The Empty field is an implementation of the Null Object Pattern
for that class/structure.

Just remember on some structures they are called something else:

Color.Empty
Point.Empty
Rectangle.Empty

DateTime.MinValue

Reading my answer I see where I could have gave a better example ;-)

Also, this pattern does not apply specifically to structures, it also
applies to classes as well, EventArgs.Empty for example.

Hope this helps
Jay
 
Hmm.. I see.

I thought it returned true if the structure wasn't initialized. You
can declare a struct like so;

Rectangle rect;

In which case it throws an error if you try to use one of its
properties. I thought the IsEmpty property checked for that, my
mistake.
 
I have an addition to what others have said.

1) Implement a public instance method "IsEmpty()"
2) Implement a public static property "Empty"
but also...
3) store a private bool field _bEmpty. IsEmpty() returns _bEmpty, and
"Empty" creates new instance with _bEmpty = true, and all other values
initialized to defaults.

Once a value type variable is initialized, it can't be uninitialized.
reference types can be set to null, but not value types. Just try setting
an integer to null. Also how do you check if a value type is initialized?
Just try this...

public struct Something
{
private int i;
public bool IsEmpty()
{
if (i == null) //compile error...
// "attempt to use before initialized" (not trapable exception)
{return true;}else{return false;}
}
}

Therefore depending on a value being uninitialized does not work.

A structure such as Point has no set of values for X and Y that would imply
emptiness. 0,0 or any other set of numbers is valid. Then any method that
returns your type can return one with _bEmpty of true when a valid return
value does not exist.

However, don't overuse empty on every kind of object. It may make more
sense to return null instead of a class instance that is "empty". Also null
uses less memory. Only, add "Empty" to a struct/class where object "state"
is important.

Michael Lang, MCSD
 
Back
Top