Home All Groups Group Topic Archive Search About

How to deserialize a SettingsProperty

Author
14 Nov 2006 7:43 AM
Serge BRIC
Hi,

I don't know how to retrieve the default value of user properties.

For instance, I have defined 2 user properties named TheColor and TheFont,
with the type System.Drawing.Color and System.Drawing.Font. I can access
their current values like this:

  dim theColor as System.Drawing.Color = My.MySettings.Default.TheColor
  dim theFont as System.Drawing.Font = My.MySettings.Default.TheColor

I have found in the SettingsProperty objects, the default value in a
serialized form :

  dim theColorProperty as SettingsProperty =
My.MySettings.Default.Properties("TheColor")
  dim theFontProperty as SettingsProperty =
My.MySettings.Default.Properties("TheFont")

  dim defaultColorValue as String = theColorProperty .DefaultValue
  dim defaultFontValue as String = theFontProperty .DefaultValue

Now, how can I build the Color and Font object containing the default values
from these SettingsProperty ? Is there a way to deserialize it ?

Thanks.

Author
14 Nov 2006 9:04 AM
Ciaran O''Donnell
Pass the color to Color.FromName() which will give you a color back. Font has
a constructor that takes the name and size (empoints).
e.g

dim defColor as Color = Color.FromName(defaultColorValue)
dim defFont as new Font(defaultFontValue, 12)


Ciaran O'Donnell


Show quote
"Serge BRIC" wrote:

> Hi,
>
> I don't know how to retrieve the default value of user properties.
>
> For instance, I have defined 2 user properties named TheColor and TheFont,
> with the type System.Drawing.Color and System.Drawing.Font. I can access
> their current values like this:
>
>   dim theColor as System.Drawing.Color = My.MySettings.Default.TheColor
>   dim theFont as System.Drawing.Font = My.MySettings.Default.TheColor
>
> I have found in the SettingsProperty objects, the default value in a
> serialized form :
>
>   dim theColorProperty as SettingsProperty =
> My.MySettings.Default.Properties("TheColor")
>   dim theFontProperty as SettingsProperty =
> My.MySettings.Default.Properties("TheFont")
>
>   dim defaultColorValue as String = theColorProperty .DefaultValue
>   dim defaultFontValue as String = theFontProperty .DefaultValue
>
> Now, how can I build the Color and Font object containing the default values
> from these SettingsProperty ? Is there a way to deserialize it ?
>
> Thanks.
>
>
>
Author
14 Nov 2006 9:13 AM
Serge BRIC
Thanks for your answer.

I understand that you suggest me to write the deserialization code by
myself. I think I could do that but I was looking for a "standard" way to
deserialize these objects ( Colors, Fonts or anything else). There must be
something like this since application and user properties are serialized and
deserialized by the ApplicationSettingsBase object.

Is there another way to retrieve default values for user properties ?


Show quote
"Ciaran O''Donnell" wrote:

> Pass the color to Color.FromName() which will give you a color back. Font has
> a constructor that takes the name and size (empoints).
> e.g
>
> dim defColor as Color = Color.FromName(defaultColorValue)
> dim defFont as new Font(defaultFontValue, 12)

>
> Ciaran O'Donnell
>
>
> "Serge BRIC" wrote:
>
> > Hi,
> >
> > I don't know how to retrieve the default value of user properties.
> >
> > For instance, I have defined 2 user properties named TheColor and TheFont,
> > with the type System.Drawing.Color and System.Drawing.Font. I can access
> > their current values like this:
> >
> >   dim theColor as System.Drawing.Color = My.MySettings.Default.TheColor
> >   dim theFont as System.Drawing.Font = My.MySettings.Default.TheColor
> >
> > I have found in the SettingsProperty objects, the default value in a
> > serialized form :
> >
> >   dim theColorProperty as SettingsProperty =
> > My.MySettings.Default.Properties("TheColor")
> >   dim theFontProperty as SettingsProperty =
> > My.MySettings.Default.Properties("TheFont")
> >
> >   dim defaultColorValue as String = theColorProperty .DefaultValue
> >   dim defaultFontValue as String = theFontProperty .DefaultValue
> >
> > Now, how can I build the Color and Font object containing the default values
> > from these SettingsProperty ? Is there a way to deserialize it ?
> >
> > Thanks.
> >
> >
> >
Author
14 Nov 2006 10:22 AM
Dave Sexton
Hi,

Try the following:

(Imports System.ComponentModel)

Dim property As SettingsProperty =
My.MySettings.Default.Properties("TheColor")

Dim converter As TypeConverter =
TypeDescriptor.GetConverter(property.PropertyType)

Dim instance As Object = converter.ConvertFromString(property.DefaultValue)

You have to cast property.DefaultValue to System.String, but I can't remember
how to do that in VB.NET - sorry

--
Dave Sexton

Show quote
"Serge BRIC" <SergeB***@discussions.microsoft.com> wrote in message
news:AA5EF2CA-5B23-44B9-A015-1ECA129EB03B@microsoft.com...
> Thanks for your answer.
>
> I understand that you suggest me to write the deserialization code by
> myself. I think I could do that but I was looking for a "standard" way to
> deserialize these objects ( Colors, Fonts or anything else). There must be
> something like this since application and user properties are serialized and
> deserialized by the ApplicationSettingsBase object.
>
> Is there another way to retrieve default values for user properties ?
>
>
> "Ciaran O''Donnell" wrote:
>
>> Pass the color to Color.FromName() which will give you a color back. Font
>> has
>> a constructor that takes the name and size (empoints).
>> e.g
>>
>> dim defColor as Color = Color.FromName(defaultColorValue)
>> dim defFont as new Font(defaultFontValue, 12)
>>
>>
>> Ciaran O'Donnell
>>
>>
>> "Serge BRIC" wrote:
>>
>> > Hi,
>> >
>> > I don't know how to retrieve the default value of user properties.
>> >
>> > For instance, I have defined 2 user properties named TheColor and
>> > TheFont,
>> > with the type System.Drawing.Color and System.Drawing.Font. I can access
>> > their current values like this:
>> >
>> >   dim theColor as System.Drawing.Color = My.MySettings.Default.TheColor
>> >   dim theFont as System.Drawing.Font = My.MySettings.Default.TheColor
>> >
>> > I have found in the SettingsProperty objects, the default value in a
>> > serialized form :
>> >
>> >   dim theColorProperty as SettingsProperty =
>> > My.MySettings.Default.Properties("TheColor")
>> >   dim theFontProperty as SettingsProperty =
>> > My.MySettings.Default.Properties("TheFont")
>> >
>> >   dim defaultColorValue as String = theColorProperty .DefaultValue
>> >   dim defaultFontValue as String = theFontProperty .DefaultValue
>> >
>> > Now, how can I build the Color and Font object containing the default
>> > values
>> > from these SettingsProperty ? Is there a way to deserialize it ?
>> >
>> > Thanks.
>> >
>> >
>> >
Author
14 Nov 2006 10:55 AM
Serge BRIC
Hi,

That's exactly what I was trying to do. I've been looking for that for many
days.

Thank you so much for your help.

Show quote
"Dave Sexton" wrote:

> Hi,
>
> Try the following:
>
> (Imports System.ComponentModel)
>
> Dim property As SettingsProperty =
> My.MySettings.Default.Properties("TheColor")
>
> Dim converter As TypeConverter =
> TypeDescriptor.GetConverter(property.PropertyType)
>
> Dim instance As Object = converter.ConvertFromString(property.DefaultValue)
>
> You have to cast property.DefaultValue to System.String, but I can't remember
> how to do that in VB.NET - sorry
>
> --
> Dave Sexton
>
> "Serge BRIC" <SergeB***@discussions.microsoft.com> wrote in message
> news:AA5EF2CA-5B23-44B9-A015-1ECA129EB03B@microsoft.com...
> > Thanks for your answer.
> >
> > I understand that you suggest me to write the deserialization code by
> > myself. I think I could do that but I was looking for a "standard" way to
> > deserialize these objects ( Colors, Fonts or anything else). There must be
> > something like this since application and user properties are serialized and
> > deserialized by the ApplicationSettingsBase object.
> >
> > Is there another way to retrieve default values for user properties ?
> >
> >
> > "Ciaran O''Donnell" wrote:
> >
> >> Pass the color to Color.FromName() which will give you a color back. Font
> >> has
> >> a constructor that takes the name and size (empoints).
> >> e.g
> >>
> >> dim defColor as Color = Color.FromName(defaultColorValue)
> >> dim defFont as new Font(defaultFontValue, 12)
> >>
> >>
> >> Ciaran O'Donnell
> >>
> >>
> >> "Serge BRIC" wrote:
> >>
> >> > Hi,
> >> >
> >> > I don't know how to retrieve the default value of user properties.
> >> >
> >> > For instance, I have defined 2 user properties named TheColor and
> >> > TheFont,
> >> > with the type System.Drawing.Color and System.Drawing.Font. I can access
> >> > their current values like this:
> >> >
> >> >   dim theColor as System.Drawing.Color = My.MySettings.Default.TheColor
> >> >   dim theFont as System.Drawing.Font = My.MySettings.Default.TheColor
> >> >
> >> > I have found in the SettingsProperty objects, the default value in a
> >> > serialized form :
> >> >
> >> >   dim theColorProperty as SettingsProperty =
> >> > My.MySettings.Default.Properties("TheColor")
> >> >   dim theFontProperty as SettingsProperty =
> >> > My.MySettings.Default.Properties("TheFont")
> >> >
> >> >   dim defaultColorValue as String = theColorProperty .DefaultValue
> >> >   dim defaultFontValue as String = theFontProperty .DefaultValue
> >> >
> >> > Now, how can I build the Color and Font object containing the default
> >> > values
> >> > from these SettingsProperty ? Is there a way to deserialize it ?
> >> >
> >> > Thanks.
> >> >
> >> >
> >> >
>
>
>

AddThis Social Bookmark Button