MJ....
As a first approximation, you can look at a structure as a "lightweight
class
with value semantics". However, unlike a class, you cannot extend a
structure,
define a destructor or initialize fields at declaration in a structure.
You cannot
define a no-arg constructor in a structure since the compiler will
insist on
doing this for you. If you do define a constructor, it is your
responsibility to
initialize all fields in the structure.
As an example, the idiom of returning a bool value and setting a
lastError
message in an instance field is not very object oriented and is not
thread
safe. Here is a prototype immutable structure aptly named BoolStruct
that is
object based and potentially thread safe.
// wraps bool result, message and source as value type
// two helper properties for bool result: B and Value
// immutable structure
public struct BoolStruct
{
public readonly bool isSuccess;
public readonly String message;
public readonly String source;
public BoolStruct(bool isSuccess,String message, String source)
{
if (message == null) {message= "";}
if (source == null) {source= "";}
this.isSuccess= isSuccess;
this.message= message;
this.source= source;
}
public bool B // helper
{
get {return isSuccess;}
}
public bool Value
{
get {return isSuccess;}
}
}
http://www.geocities.com/Jeff_Louie/OOP/oop11.htm
Regards,
Jeff
*** Sent via Developersdex
http://www.developersdex.com ***