TypedDefaultValue Class

Posts   
 
    
jshallard
User
Posts: 62
Joined: 23-Mar-2005
# Posted on: 16-Jun-2005 18:43:49   

Is there an easy way to determine if a the value of an Entity Property is still set to the default value?

I have found in the past that it is often useful to have a class with publicly accessible static members containing the default value for each System Type. This way it is easy to check if a value is still default by comparing it to this value.

By adding the following code to the Custom_TypeDefaultValueTemplate Template, this can easily be achieved.


                        public static System.String NullString        = String.Empty;
            public static System.Byte NullByte         = (byte)0;
            public static System.Int32 NullInt32          = (int)0;
            public static System.Int16 NullInt16          = (short)0;
            public static System.Int64 NullInt64          = (long)0;
            public static System.DateTime NullDateTime  = DateTime.MinValue;
            public static System.Decimal NullDecimal    = 0.0M;
            public static System.Double NullDouble      = 0.0;
            public static System.Single NullSingle         = 0.0f;
            public static System.Boolean NullBoolean      = false;
            public static System.Byte[] NullByteArray   = new byte[0];
            public static System.Guid NullGuid          = Guid.Empty;
            public static System.Object NullObject        = new object();

Then to check if a value is default, the following code is all that is needed:

if(Entity.StringProperty.equals(TypeDefaultValue.NullString)){...

Any comments on this welcome.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 16-Jun-2005 19:02:22   

Yes. if entity.Fields[index].IsChanged is true, hte field is not set to its default value. If the IsChanged is false and the entity is new, the field is still set to its default value. if the entity isn't new (entity.IsNew is false) and the field isn't changed, and entity.Fields[index].IsNull is true, the field is still set to its default value.

Though I think you don't have to check for that, just check if the field is changed or not.

Frans Bouma | Lead developer LLBLGen Pro