List Info

Thread: Determine type default value




Determine type default value
user name
2006-08-11 07:13:33
Is there anyway to determine what the default value of a
type will be?
I'm using reflection to extract a value from an object's
property
accessor - but I need to know whether the value I've got
would be the
property types default value. I.e. if the property type is
Int32 if I
get zero I know this is the default value. I don't need to
know whether
zero was assigned all I need to know is whether it's the
types default
value.

 

Rob.

 


===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Determine type default value
user name
2006-08-11 07:33:35
Robert Rolls <Robert.RollsDEVNET.ATO.GOV.AU>
wrote:

> Is there anyway to determine what the default value of
a type will be?

What do you mean by default? For value types, the
'default' value is all
zeros. For reference types, the 'default' value is null.
Do you have
some different default in mind?

> I don't need to know whether
> zero was assigned all I need to know is whether it's
the types default
> value.

Compare it (using object.Equals(object,object)) with a
default value,
however you get one. For example, for value types, you can
instantiate a
new one with Activator.CreateInstance(). ValueType overrides
object.Equals(object) in order to do a field by field
comparison. For
reference types, either you take 'null' as being the
default value, or
you rely on it being safe to construct a new instance, and
rely on
object.Equals(object) being correctly overridden.

-- Barry

-- 
http://barrkel.blogspot.
com/

===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Determine type default value
user name
2006-08-11 07:49:31
On 11/08/06, Robert Rolls <Robert.Rollsdevnet.ato.gov.au> wrote:
> Is there anyway to determine what the default value of
a type will be?
> I'm using reflection to extract a value from an
object's property
> accessor - but I need to know whether the value I've
got would be the
> property types default value. I.e. if the property type
is Int32 if I
> get zero I know this is the default value. I don't
need to know whether
> zero was assigned all I need to know is whether it's
the types default
> value.

It depends what you mean by default, and what you want the
information for.

If you're talking about an uninitialised value type, then I
guess you
can always instantiate an object of the same type using a
parameterless constructor, and perform a comparison.

That doesn't really help for object types - you're
dependent upon
there being a suitable constructor and suitable equality
override
providing value-compare semantics.

If you're taking the value from a hosting object, you also
appear to
have overlooked the possibility (or not care that) that the
hosting
object may have chosen to initialise the value to what it
considers as
the 'default' value (e.g. an HttpRequest might have a
Timeout
property, with a default value of 30).

The PropertyGrid uses the DefaultValueAttribute of the
property to
indicate the default value (that's how it decides whether
to display
the value in normal text or bold).

John

===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Determine type default value
user name
2006-08-11 07:50:20
For value type the default value will always be a zeroed out
structure. For
primitive types in particular 0.
For reference types you cant always say there is a default
value as the
creation of instances might yield non equl/equivalent
instances. take for
example a type wich in its default constructor initializes
the fields based
on some enviroment variables like time.

But for the purposese of finding out wha't a property's
default value the
DefaultValueAttribute is used. So all you have to do is find
out if the
property has a DefaultValueAttibute assigned and use that
atrribute to
retrive an instance the type wich is the property's default
value.

On 8/11/06, Robert Rolls <Robert.Rollsdevnet.ato.gov.au> wrote:
>
> Is there anyway to determine what the default value of
a type will be?
> I'm using reflection to extract a value from an
object's property
> accessor - but I need to know whether the value I've
got would be the
> property types default value. I.e. if the property type
is Int32 if I
> get zero I know this is the default value. I don't
need to know whether
> zero was assigned all I need to know is whether it's
the types default
> value.
>
>
>
> Rob.
>
>
>
>
> ===================================
> This list is hosted by DevelopMentor(r)  http://www.develop.com
>
> View archives and manage your subscription(s) at
> http://discuss.develop.com

>

===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Determine type default value
user name
2006-08-11 07:43:51
> Is there anyway to determine what the default value of
a type will be?
> I'm using reflection to extract a value from an
object's
> property accessor - but I need to know whether the
value I've
> got would be the property types default value. I.e. if
the
> property type is Int32 if I get zero I know this is the
> default value. I don't need to know whether zero was
assigned
> all I need to know is whether it's the types default
value.

        Couldn't you compare the value with a value you
pull from a defaults list?

        Anyway, I'd advice against 'magic' values for
'default's and also for nulls. Sooner or later your app
will need the value
you set for default as a valid value.

                FB

===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Determine type default value
user name
2006-08-11 09:08:39
It should suffice that all I need to know is if the property
type is a value
type that the value will be equal to 0 and null if a
reference type I can
safely assume that it's the properties default value.

I wouldn't need to instantiate any given my simple rule
above?

(Yes presumably in the future I could require some other
value to represent
a default but that's another requirement that will get
scoped accordingly at
the moment it's clearly document as an assumption - oh I
just love
assumptions.)


-----Original Message-----
From: Discussion of development on the .NET platform using
any managed
language [mailtoOTNET-CL
RDISCUSS.DEVELOP.COM] On Behalf Of Barry Kelly
Sent: Friday, August 11, 2006 5:34 PM
To: DOTNET-CLRDISCUSS.DEVELOP.COM
Subject: Re: [DOTNET-CLR] Determine type default value

Robert Rolls <Robert.RollsDEVNET.ATO.GOV.AU>
wrote:

> Is there anyway to determine what the default value of
a type will be?

What do you mean by default? For value types, the
'default' value is all
zeros. For reference types, the 'default' value is null.
Do you have
some different default in mind?

> I don't need to know whether
> zero was assigned all I need to know is whether it's
the types default
> value.

Compare it (using object.Equals(object,object)) with a
default value,
however you get one. For example, for value types, you can
instantiate a
new one with Activator.CreateInstance(). ValueType overrides
object.Equals(object) in order to do a field by field
comparison. For
reference types, either you take 'null' as being the
default value, or
you rely on it being safe to construct a new instance, and
rely on
object.Equals(object) being correctly overridden.

-- Barry

--
http://barrkel.blogspot.
com/

===================================
This list is hosted by DevelopMentor.  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com


===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Determine type default value
user name
2006-08-11 09:09:23
I wonder Default<> must know internally what to do?

-----Original Message-----
From: Discussion of development on the .NET platform using
any managed
language [mailtoOTNET-CL
RDISCUSS.DEVELOP.COM] On Behalf Of Barry Kelly
Sent: Friday, August 11, 2006 5:34 PM
To: DOTNET-CLRDISCUSS.DEVELOP.COM
Subject: Re: [DOTNET-CLR] Determine type default value

Robert Rolls <Robert.RollsDEVNET.ATO.GOV.AU>
wrote:

> Is there anyway to determine what the default value of
a type will be?

What do you mean by default? For value types, the
'default' value is all
zeros. For reference types, the 'default' value is null.
Do you have
some different default in mind?

> I don't need to know whether
> zero was assigned all I need to know is whether it's
the types default
> value.

Compare it (using object.Equals(object,object)) with a
default value,
however you get one. For example, for value types, you can
instantiate a
new one with Activator.CreateInstance(). ValueType overrides
object.Equals(object) in order to do a field by field
comparison. For
reference types, either you take 'null' as being the
default value, or
you rely on it being safe to construct a new instance, and
rely on
object.Equals(object) being correctly overridden.

-- Barry

--
http://barrkel.blogspot.
com/

===================================
This list is hosted by DevelopMentor.  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com


===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

[1-7]

about | contact  Other archives ( Real Estate discussion Medical topics )