Specifying property defaults

Discussion in 'ASP .Net Web Controls' started by Nathan Sokalski, Mar 22, 2009.

  1. I need to specify the default value for a property of a custom control I am
    making. Normally this would be done as follows:

    Private _myprop As String = "My Default Value"

    <System.ComponentModel.DefaultValue("My Default Value")> Public Property
    MyProperty() As String
    Get
    Return Me._myprop
    End Get
    Set(ByVal value As String)
    Me._myprop = value
    End Set
    End Property


    However, in my situation I am not using a private variable to store the
    value. If I understand correctly, the Property -> Sets for any properties
    specified is the first thing to occur in the control. Is there a way to
    specify a default value without using the technique I show above? Thanks.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/
     
    Nathan Sokalski, Mar 22, 2009
    #1
    1. Advertising

  2. Nathan Sokalski

    bruce barker Guest

    you can do it in the constructor, or if thats too early, do it in the
    get/set methods.


    bool myPropInited = false;
    string myProp
    {
    get
    {
    if (!myPropInited)
    {
    setDefaultValue();
    myPropInited = true;
    }
    return myPropvalue;

    }
    set
    {
    myPropInited = true;
    .....
    }
    }


    -- bruce (sqlwork.com)


    Nathan Sokalski wrote:
    > I need to specify the default value for a property of a custom control I am
    > making. Normally this would be done as follows:
    >
    > Private _myprop As String = "My Default Value"
    >
    > <System.ComponentModel.DefaultValue("My Default Value")> Public Property
    > MyProperty() As String
    > Get
    > Return Me._myprop
    > End Get
    > Set(ByVal value As String)
    > Me._myprop = value
    > End Set
    > End Property
    >
    >
    > However, in my situation I am not using a private variable to store the
    > value. If I understand correctly, the Property -> Sets for any properties
    > specified is the first thing to occur in the control. Is there a way to
    > specify a default value without using the technique I show above? Thanks.
     
    bruce barker, Mar 22, 2009
    #2
    1. Advertising

  3. Nathan

    This is a verry good article to read for you

    http://www.dotnetarticles.net/Graphics-Programming/657.html

    Keep in mind that you still need a way to send a message to the IDE for
    your default property value to show ( 2 ways described in the article )

    HTH

    Michel


    "Nathan Sokalski" <> schreef in bericht
    news:%...
    >I need to specify the default value for a property of a custom control I am
    >making. Normally this would be done as follows:
    >
    > Private _myprop As String = "My Default Value"
    >
    > <System.ComponentModel.DefaultValue("My Default Value")> Public Property
    > MyProperty() As String
    > Get
    > Return Me._myprop
    > End Get
    > Set(ByVal value As String)
    > Me._myprop = value
    > End Set
    > End Property
    >
    >
    > However, in my situation I am not using a private variable to store the
    > value. If I understand correctly, the Property -> Sets for any properties
    > specified is the first thing to occur in the control. Is there a way to
    > specify a default value without using the technique I show above? Thanks.
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
    >
     
    Michel Posseth [MCP], Mar 22, 2009
    #3
  4. Nathan Sokalski

    James Hahn Guest

    Note that the article is out of date "For cases where the properties return
    some type other than a string, a number, or an enumeration,..".

    The default property can be set by using a string representation of the
    property value in conjunction with the type. Eg:

    DefaultValue(GetType(Font), "Microsoft Sans Serif, 8.25pt")
    DefaultValue(GetType(Color), "Orange")

    "Michel Posseth [MCP]" <> wrote in message
    news:...
    > Nathan
    >
    > This is a verry good article to read for you
    >
    > http://www.dotnetarticles.net/Graphics-Programming/657.html
    >
    > Keep in mind that you still need a way to send a message to the IDE for
    > your default property value to show ( 2 ways described in the article )
    >
    > HTH
    >
    > Michel
    >
    >
    > "Nathan Sokalski" <> schreef in bericht
    > news:%...
    >>I need to specify the default value for a property of a custom control I
    >>am making. Normally this would be done as follows:
    >>
    >> Private _myprop As String = "My Default Value"
    >>
    >> <System.ComponentModel.DefaultValue("My Default Value")> Public Property
    >> MyProperty() As String
    >> Get
    >> Return Me._myprop
    >> End Get
    >> Set(ByVal value As String)
    >> Me._myprop = value
    >> End Set
    >> End Property
    >>
    >>
    >> However, in my situation I am not using a private variable to store the
    >> value. If I understand correctly, the Property -> Sets for any properties
    >> specified is the first thing to occur in the control. Is there a way to
    >> specify a default value without using the technique I show above? Thanks.
    >> --
    >> Nathan Sokalski
    >>
    >> http://www.nathansokalski.com/
    >>

    >
    >
     
    James Hahn, Mar 22, 2009
    #4
  5. Maybe I am missing something in the article, but let me be more specific
    about my situation. I am inheriting from the
    System.Web.UI.HtmlControls.HtmlAnchor class. I want to be able to set the
    inherited Target property to "_blank" or String.Empty based on a Boolean
    value specified by the user. Here is my current code:


    <System.ComponentModel.DefaultValue(True)> Public Property TargetBlank() As
    Boolean
    Get
    Return Me.Target.ToLower() = "_blank"
    End Get
    Set(ByVal value As Boolean)
    If value Then Me.Target = "_blank" Else Me.Target = String.Empty
    End Set
    End Property


    This will work if the user specifies a value, but because in order to have
    the default be Me.Target="_blank" the Set method must be run and I cannot
    run any code before the Property -> Set methods are run, I cannot find a way
    to set the default.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "Michel Posseth [MCP]" <> wrote in message
    news:...
    > Nathan
    >
    > This is a verry good article to read for you
    >
    > http://www.dotnetarticles.net/Graphics-Programming/657.html
    >
    > Keep in mind that you still need a way to send a message to the IDE for
    > your default property value to show ( 2 ways described in the article )
    >
    > HTH
    >
    > Michel
    >
    >
    > "Nathan Sokalski" <> schreef in bericht
    > news:%...
    >>I need to specify the default value for a property of a custom control I
    >>am making. Normally this would be done as follows:
    >>
    >> Private _myprop As String = "My Default Value"
    >>
    >> <System.ComponentModel.DefaultValue("My Default Value")> Public Property
    >> MyProperty() As String
    >> Get
    >> Return Me._myprop
    >> End Get
    >> Set(ByVal value As String)
    >> Me._myprop = value
    >> End Set
    >> End Property
    >>
    >>
    >> However, in my situation I am not using a private variable to store the
    >> value. If I understand correctly, the Property -> Sets for any properties
    >> specified is the first thing to occur in the control. Is there a way to
    >> specify a default value without using the technique I show above? Thanks.
    >> --
    >> Nathan Sokalski
    >>
    >> http://www.nathansokalski.com/
    >>

    >
    >
     
    Nathan Sokalski, Mar 22, 2009
    #5
  6. Nathan Sokalski

    Guest

    On Mar 22, 6:39 pm, "Nathan Sokalski" <> wrote:
    > Maybe I am missing something in the article, but let me be more specific
    > about my situation. I am inheriting from the
    > System.Web.UI.HtmlControls.HtmlAnchor class. I want to be able to set the
    > inherited Target property to "_blank" or String.Empty based on a Boolean
    > value specified by the user. Here is my current code:
    >
    > <System.ComponentModel.DefaultValue(True)> Public Property TargetBlank() As
    > Boolean
    >  Get
    >   Return Me.Target.ToLower() = "_blank"
    >  End Get
    >  Set(ByVal value As Boolean)
    >   If value Then Me.Target = "_blank" Else Me.Target = String.Empty
    >  End Set
    > End Property
    >
    > This will work if the user specifies a value, but because in order to have
    > the default be Me.Target="_blank" the Set method must be run and I cannot
    > run any code before the Property -> Set methods are run, I cannot find a way
    > to set the default.
    > --
    > Nathan Sokalski
    > ://www.nathansokalski.com/
    >
    > "Michel Posseth [MCP]" <> wrote in messagenews:...
    >
    >
    >
    > > Nathan

    >
    > > This is a verry good article to read for you

    >
    > >http://www.dotnetarticles.net/Graphics-Programming/657.html

    >
    > > Keep in mind that you still need a way to send a  message to the IDE for
    > > your default property value to show ( 2 ways described in the article )

    >
    > > HTH

    >
    > > Michel

    >
    > > "Nathan Sokalski" <> schreef in bericht
    > >news:%...
    > >>I need to specify the default value for a property of a custom control I
    > >>am making. Normally this would be done as follows:

    >
    > >> Private _myprop As String = "My Default Value"

    >
    > >> <System.ComponentModel.DefaultValue("My Default Value")> Public Property
    > >> MyProperty() As String
    > >> Get
    > >>  Return Me._myprop
    > >> End Get
    > >> Set(ByVal value As String)
    > >>  Me._myprop = value
    > >> End Set
    > >> End Property

    >
    > >> However, in my situation I am not using a private variable to store the
    > >> value. If I understand correctly, the Property -> Sets for any properties
    > >> specified is the first thing to occur in the control. Is there a way to
    > >> specify a default value without using the technique I show above? Thanks.


    Not sure if it will be acceptable or not, but you could do this by the
    user passing a parameter to the control's contructor.
     
    , Mar 22, 2009
    #6
  7. That would work with my current code, but my goal here is to set a default
    value (in other words, specify what the value will be when the user does not
    pass a parameter).
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    <> wrote in message
    news:...
    On Mar 22, 6:39 pm, "Nathan Sokalski" <> wrote:
    > Maybe I am missing something in the article, but let me be more specific
    > about my situation. I am inheriting from the
    > System.Web.UI.HtmlControls.HtmlAnchor class. I want to be able to set the
    > inherited Target property to "_blank" or String.Empty based on a Boolean
    > value specified by the user. Here is my current code:
    >
    > <System.ComponentModel.DefaultValue(True)> Public Property TargetBlank()
    > As
    > Boolean
    > Get
    > Return Me.Target.ToLower() = "_blank"
    > End Get
    > Set(ByVal value As Boolean)
    > If value Then Me.Target = "_blank" Else Me.Target = String.Empty
    > End Set
    > End Property
    >
    > This will work if the user specifies a value, but because in order to have
    > the default be Me.Target="_blank" the Set method must be run and I cannot
    > run any code before the Property -> Set methods are run, I cannot find a
    > way
    > to set the default.
    > --
    > Nathan Sokalski
    > ://www.nathansokalski.com/
    >
    > "Michel Posseth [MCP]" <> wrote in
    > messagenews:...
    >
    >
    >
    > > Nathan

    >
    > > This is a verry good article to read for you

    >
    > >http://www.dotnetarticles.net/Graphics-Programming/657.html

    >
    > > Keep in mind that you still need a way to send a message to the IDE for
    > > your default property value to show ( 2 ways described in the article )

    >
    > > HTH

    >
    > > Michel

    >
    > > "Nathan Sokalski" <> schreef in bericht
    > >news:%...
    > >>I need to specify the default value for a property of a custom control I
    > >>am making. Normally this would be done as follows:

    >
    > >> Private _myprop As String = "My Default Value"

    >
    > >> <System.ComponentModel.DefaultValue("My Default Value")> Public
    > >> Property
    > >> MyProperty() As String
    > >> Get
    > >> Return Me._myprop
    > >> End Get
    > >> Set(ByVal value As String)
    > >> Me._myprop = value
    > >> End Set
    > >> End Property

    >
    > >> However, in my situation I am not using a private variable to store the
    > >> value. If I understand correctly, the Property -> Sets for any
    > >> properties
    > >> specified is the first thing to occur in the control. Is there a way to
    > >> specify a default value without using the technique I show above?
    > >> Thanks.


    Not sure if it will be acceptable or not, but you could do this by the
    user passing a parameter to the control's contructor.
     
    Nathan Sokalski, Mar 23, 2009
    #7
  8. Nathan Sokalski

    Guest

    On Mar 22, 8:19 pm, "Nathan Sokalski" <> wrote:
    > That would work with my current code, but my goal here is to set a default
    > value (in other words, specify what the value will be when the user does not
    > pass a parameter).


    What you deescribe sounds exactly like setting the property to a
    constant value in the constructor.

    > --
    > Nathan Sokalski
    > ://www.nathansokalski.com/
    >
    > <> wrote in message
    >
    > news:...
    > On Mar 22, 6:39 pm, "Nathan Sokalski" <> wrote:
    >
    >
    >
    >
    >
    > > Maybe I am missing something in the article, but let me be more specific
    > > about my situation. I am inheriting from the
    > > System.Web.UI.HtmlControls.HtmlAnchor class. I want to be able to set the
    > > inherited Target property to "_blank" or String.Empty based on a Boolean
    > > value specified by the user. Here is my current code:

    >
    > > <System.ComponentModel.DefaultValue(True)> Public Property TargetBlank()
    > > As
    > > Boolean
    > > Get
    > > Return Me.Target.ToLower() = "_blank"
    > > End Get
    > > Set(ByVal value As Boolean)
    > > If value Then Me.Target = "_blank" Else Me.Target = String.Empty
    > > End Set
    > > End Property

    >
    > > This will work if the user specifies a value, but because in order to have
    > > the default be Me.Target="_blank" the Set method must be run and I cannot
    > > run any code before the Property -> Set methods are run, I cannot find a
    > > way
    > > to set the default.
    > > --
    > > Nathan Sokalski
    > > ://www.nathansokalski.com/

    >
    > > "Michel Posseth [MCP]" <> wrote in
    > > messagenews:...

    >
    > > > Nathan

    >
    > > > This is a verry good article to read for you

    >
    > > >http://www.dotnetarticles.net/Graphics-Programming/657.html

    >
    > > > Keep in mind that you still need a way to send a message to the IDE for
    > > > your default property value to show ( 2 ways described in the article)

    >
    > > > HTH

    >
    > > > Michel

    >
    > > > "Nathan Sokalski" <> schreef in bericht
    > > >news:%...
    > > >>I need to specify the default value for a property of a custom control I
    > > >>am making. Normally this would be done as follows:

    >
    > > >> Private _myprop As String = "My Default Value"

    >
    > > >> <System.ComponentModel.DefaultValue("My Default Value")> Public
    > > >> Property
    > > >> MyProperty() As String
    > > >> Get
    > > >> Return Me._myprop
    > > >> End Get
    > > >> Set(ByVal value As String)
    > > >> Me._myprop = value
    > > >> End Set
    > > >> End Property

    >
    > > >> However, in my situation I am not using a private variable to store the
    > > >> value. If I understand correctly, the Property -> Sets for any
    > > >> properties
    > > >> specified is the first thing to occur in the control. Is there a wayto
    > > >> specify a default value without using the technique I show above?
    > > >> Thanks.

    >
    > Not sure if it will be acceptable or not, but you could do this by the
    > user passing a parameter to the control's contructor.
     
    , Mar 23, 2009
    #8
  9. I currently do not have a constructor (since the only values that get set
    during creation are the attributes specified in the control in the aspx
    file, I have never needed one when creating a custom control before). But I
    tried your idea and added

    Public Sub New()
    MyBase.New()
    Me.Target = "_blank"
    End Sub

    And it worked! Thank you, you are a life saver!
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    <> wrote in message
    news:...
    On Mar 22, 8:19 pm, "Nathan Sokalski" <> wrote:
    > That would work with my current code, but my goal here is to set a default
    > value (in other words, specify what the value will be when the user does
    > not
    > pass a parameter).


    What you deescribe sounds exactly like setting the property to a
    constant value in the constructor.

    > --
    > Nathan Sokalski
    > ://www.nathansokalski.com/
    >
    > <> wrote in message
    >
    > news:...
    > On Mar 22, 6:39 pm, "Nathan Sokalski" <> wrote:
    >
    >
    >
    >
    >
    > > Maybe I am missing something in the article, but let me be more specific
    > > about my situation. I am inheriting from the
    > > System.Web.UI.HtmlControls.HtmlAnchor class. I want to be able to set
    > > the
    > > inherited Target property to "_blank" or String.Empty based on a Boolean
    > > value specified by the user. Here is my current code:

    >
    > > <System.ComponentModel.DefaultValue(True)> Public Property TargetBlank()
    > > As
    > > Boolean
    > > Get
    > > Return Me.Target.ToLower() = "_blank"
    > > End Get
    > > Set(ByVal value As Boolean)
    > > If value Then Me.Target = "_blank" Else Me.Target = String.Empty
    > > End Set
    > > End Property

    >
    > > This will work if the user specifies a value, but because in order to
    > > have
    > > the default be Me.Target="_blank" the Set method must be run and I
    > > cannot
    > > run any code before the Property -> Set methods are run, I cannot find a
    > > way
    > > to set the default.
    > > --
    > > Nathan Sokalski
    > > ://www.nathansokalski.com/

    >
    > > "Michel Posseth [MCP]" <> wrote in
    > > messagenews:...

    >
    > > > Nathan

    >
    > > > This is a verry good article to read for you

    >
    > > >http://www.dotnetarticles.net/Graphics-Programming/657.html

    >
    > > > Keep in mind that you still need a way to send a message to the IDE
    > > > for
    > > > your default property value to show ( 2 ways described in the
    > > > article )

    >
    > > > HTH

    >
    > > > Michel

    >
    > > > "Nathan Sokalski" <> schreef in bericht
    > > >news:%...
    > > >>I need to specify the default value for a property of a custom control
    > > >>I
    > > >>am making. Normally this would be done as follows:

    >
    > > >> Private _myprop As String = "My Default Value"

    >
    > > >> <System.ComponentModel.DefaultValue("My Default Value")> Public
    > > >> Property
    > > >> MyProperty() As String
    > > >> Get
    > > >> Return Me._myprop
    > > >> End Get
    > > >> Set(ByVal value As String)
    > > >> Me._myprop = value
    > > >> End Set
    > > >> End Property

    >
    > > >> However, in my situation I am not using a private variable to store
    > > >> the
    > > >> value. If I understand correctly, the Property -> Sets for any
    > > >> properties
    > > >> specified is the first thing to occur in the control. Is there a way
    > > >> to
    > > >> specify a default value without using the technique I show above?
    > > >> Thanks.

    >
    > Not sure if it will be acceptable or not, but you could do this by the
    > user passing a parameter to the control's contructor.
     
    Nathan Sokalski, Mar 23, 2009
    #9
  10. "Nathan Sokalski" <> schrieb
    > <System.ComponentModel.DefaultValue(True)> Public Property TargetBlank()
    > As Boolean
    > Get
    > Return Me.Target.ToLower() = "_blank"
    > End Get
    > Set(ByVal value As Boolean)
    > If value Then Me.Target = "_blank" Else Me.Target = String.Empty
    > End Set
    > End Property
    >
    >
    > This will work if the user specifies a value, but because in order to have
    > the default be Me.Target="_blank" the Set method must be run and I cannot
    > run any code before the Property -> Set methods are run, I cannot find a
    > way to set the default.


    As this seems to be redundant information, why is "Target" not just a
    Readonly property that returns the one ("_blank") or the other
    (string.empty) value depending on the value of the TargetBlank property?


    Armin
     
    Armin Zingler, Mar 23, 2009
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Fox

    Changing VS.NET defaults

    Mark Fox, Jul 31, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    366
    Yan-Hong Huang[MSFT]
    Aug 4, 2003
  2. Lauchlan M
    Replies:
    2
    Views:
    496
    John Saunders
    Aug 17, 2003
  3. John A Grandy
    Replies:
    0
    Views:
    325
    John A Grandy
    Oct 16, 2003
  4. Nathan Sokalski

    Specifying property defaults

    Nathan Sokalski, Mar 22, 2009, in forum: ASP .Net
    Replies:
    9
    Views:
    397
    Armin Zingler
    Mar 23, 2009
  5. Helen

    Specifying a control as a tag property

    Helen, Jul 1, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    125
    Helen
    Jul 6, 2004
Loading...

Share This Page