views:

45

answers:

2

I know you can add defaultValues using the web.config like this:

<profile>
    <properties>
        <add name="AreCool" type="System.Boolean" defaultValue="False" />
    </properties>
</profile>

but I have the Profile inherited from a class:

<profile inherits="CustomProfile" defaultProvider="CustomProfileProvider" enabled="true">
  <providers>
    <clear />
    <add name="CustomProfileProvider" type="CustomProfileProvider" />
  </providers>
</profile>

Heres the class:

Public Class CustomProfile
    Inherits ProfileBase

    Public Property AreCool() As Boolean
        Get
            Return Me.GetPropertyValue("AreCool")
        End Get
        Set(ByVal value As Boolean)
            Me.SetPropertyValue("AreCool", value)
        End Set
    End Property

End Class

I don't know how to set the default value of the property. Its causing errors because without a default value, it uses an empty string, which cannot be converted to a Boolean. I tried adding <DefaultSettingValue("False")> _ but that didn't seem to make a difference.

I'm also using a custom ProfileProvider (CustomProfileProvider).

A: 

Just a thought, can you do something like this or some variation (meaning instead of .length, use dbnull.value() or however you need to check if it is an actual item?

Edited code to handle empty set parameters

Public Class CustomProfile Inherits ProfileBase

Dim _outBool as boolean
Public Property AreCool() As Boolean 
    Get 
        Return Me.GetPropertyValue("AreCool")
    End Get 
    Set(ByVal value As Object) 
         ''if the value can be parsed to boolean, set AreCool to value, else default to false''
         If([Boolean].TryParse(value, outBool) Then
            Me.SetPropertyValue("AreCool", value)
        Else
            Me.SetPropertyValue("AreCool", False)
        End If

    End Set 
End Property 

End Class

Tommy
i think the errors are coming from the Set, "" errors as value for the Boolean parameter
mr.moses
Updated the Set portion. You can do logic checks here as well if you need to do a manual override on the setting of the property. Here, using boolean.tryParse instead of a string length.
Tommy
it doesn't even enter the Set function because it errors when converting an empty string to a Boolean for the value parameter
mr.moses
Are you populating this from a database table or within your code?
Tommy
The Profile property values are coming from an CMS API.
mr.moses
Then change your input Set to an Object type and then perform your checks - code updated
Tommy
A: 

The typical way that Microsoft does this throughout the .NET framework is to use the get portion to check to see if the value can be converted and returning the default if it cannot. For example:

Public Class CustomProfile 
    Inherits ProfileBase 

    Public Property AreCool() As Boolean 
        Get 
            Dim o as Object = Me.GetPropertyValue("AreCool") 
            If TypeOf o Is Boolean Then
                Return CBool(o)
            End If
            Return False 'Return the default
        End Get 
        Set(ByVal value As Boolean) 
            Me.SetPropertyValue("AreCool", value) 
        End Set 
    End Property 

End Class
NightOwl888