locked
Issues with Properties: DefaultValue & Boolean Types RRS feed

  • Question

  • User1947866343 posted

    I'm creating a custom web control to display weather from an xml feed.  I have a couple questions regarding properties. 

    1.  I have a boolean property called "ShowForecast".  If I set the default value to True or False, it always returns false when viewed in the browser.  However, if I set the default value to "", it works every time...?  I'm curious if anyone can give me some tips about DefaultValues of properties.

     Works:

         Private _showForecast AS Boolean
    <Category("Appearance"), DefaultValue("")> _
    Public Property ShowForecast() As Boolean
    Get
    Return
    _showForecast
    End Get
    Set
    (ByVal value As Boolean)
    _showForecast = value
    End Set
    End Property

      

    Doesn't work:

         Private _showForecast AS Boolean
    <Category("Appearance"), DefaultValue(True)> _
    Public Property ShowForecast() As Boolean
    Get
    Return
    _showForecast
    End Get
    Set
    (ByVal value As Boolean)
    _showForecast = value
    End Set
    End Property

      

    2. I have a string property with a default value of "Images".  I would like "Images" to appear in the property pane the first time the control is dropped onto an aspx/ascx from the toolbox.  No matter what I put in the "DefaultValue" field in my code, they always show up blank.  Any thoughts?

    Wednesday, March 26, 2008 11:07 AM

Answers

  • User1340733228 posted

    The default value attribute does not actually set the default value for the property as you would think (see http://support.microsoft.com/kb/311339) So if you want the _showForecast to default to true, you need to initialize the _showForecast variable to true. Then, by setting the default value attribute to true, the IDE will not bold it in the properties pane and the extra code will not be generated. If you change the value to false in the IDE, you will see it is in bold and the property will be generated in the code.

    Hope that helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 26, 2008 1:25 PM

All replies

  • User1340733228 posted

    The default value attribute does not actually set the default value for the property as you would think (see http://support.microsoft.com/kb/311339) So if you want the _showForecast to default to true, you need to initialize the _showForecast variable to true. Then, by setting the default value attribute to true, the IDE will not bold it in the properties pane and the extra code will not be generated. If you change the value to false in the IDE, you will see it is in bold and the property will be generated in the code.

    Hope that helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 26, 2008 1:25 PM
  • User1947866343 posted

     That's exactly what I was looking for...thanks!

    Wednesday, March 26, 2008 3:38 PM