|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Specifying property defaultsmaking. 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. 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: Show quoteHide quote > 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
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 Show quoteHide quote "Nathan Sokalski" <njsokal***@hotmail.com> schreef in bericht news:%23bk4Y2xqJHA.1504@TK2MSFTNGP03.phx.gbl... >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 > njsokal***@hotmail.com > http://www.nathansokalski.com/ > 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") Show quoteHide quote "Michel Posseth [MCP]" <M***@posseth.com> wrote in message news:ua3YsVyqJHA.4996@TK2MSFTNGP04.phx.gbl... > 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" <njsokal***@hotmail.com> schreef in bericht > news:%23bk4Y2xqJHA.1504@TK2MSFTNGP03.phx.gbl... >>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 >> njsokal***@hotmail.com >> http://www.nathansokalski.com/ >> > > 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. Show quoteHide quote "Michel Posseth [MCP]" <M***@posseth.com> wrote in message news:ua3YsVyqJHA.4996@TK2MSFTNGP04.phx.gbl... > 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" <njsokal***@hotmail.com> schreef in bericht > news:%23bk4Y2xqJHA.1504@TK2MSFTNGP03.phx.gbl... >>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 >> njsokal***@hotmail.com >> http://www.nathansokalski.com/ >> > >
Show quote
Hide quote
On Mar 22, 6:39 pm, "Nathan Sokalski" <njsokal***@hotmail.com> wrote: Not sure if it will be acceptable or not, but you could do this by the> 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 > njsokal...@hotmail.comhttp://www.nathansokalski.com/ > > "Michel Posseth [MCP]" <M***@posseth.com> wrote in messagenews:ua3YsVyqJHA.4***@TK2MSFTNGP04.phx.gbl... > > > > > 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" <njsokal***@hotmail.com> schreef in bericht > >news:%23bk4Y2xqJHA.1504@TK2MSFTNGP03.phx.gbl... > >>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. user passing a parameter to the control's contructor. 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). -- Show quoteHide quoteNathan Sokalski njsokal***@hotmail.com http://www.nathansokalski.com/ <joecool1***@live.com> wrote in message news:b183820f-8371-46ef-aae0-a2236ad85bd7@n30g2000vba.googlegroups.com... Not sure if it will be acceptable or not, but you could do this by theOn Mar 22, 6:39 pm, "Nathan Sokalski" <njsokal***@hotmail.com> 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 > njsokal...@hotmail.comhttp://www.nathansokalski.com/ > > "Michel Posseth [MCP]" <M***@posseth.com> wrote in > messagenews:ua3YsVyqJHA.4***@TK2MSFTNGP04.phx.gbl... > > > > > 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" <njsokal***@hotmail.com> schreef in bericht > >news:%23bk4Y2xqJHA.1504@TK2MSFTNGP03.phx.gbl... > >>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. user passing a parameter to the control's contructor. On Mar 22, 8:19 pm, "Nathan Sokalski" <njsokal***@hotmail.com> wrote: What you deescribe sounds exactly like setting the property to a> 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). constant value in the constructor. Show quoteHide quote > -- > Nathan Sokalski > njsokal...@hotmail.comhttp://www.nathansokalski.com/ > > <joecool1***@live.com> wrote in message > > news:b183820f-8371-46ef-aae0-a2236ad85bd7@n30g2000vba.googlegroups.com... > On Mar 22, 6:39 pm, "Nathan Sokalski" <njsokal***@hotmail.com> 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 > > njsokal...@hotmail.comhttp://www.nathansokalski.com/ > > > "Michel Posseth [MCP]" <M***@posseth.com> wrote in > > messagenews:ua3YsVyqJHA.4***@TK2MSFTNGP04.phx.gbl... > > > > 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" <njsokal***@hotmail.com> schreef in bericht > > >news:%23bk4Y2xqJHA.1504@TK2MSFTNGP03.phx.gbl... > > >>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. 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 njsokal***@hotmail.com http://www.nathansokalski.com/ <joecool1***@live.com> wrote in message news:6b50e872-a77a-4cba-bc3a-019f8d05fb75@e5g2000vbe.googlegroups.com... What you deescribe sounds exactly like setting the property to aOn Mar 22, 8:19 pm, "Nathan Sokalski" <njsokal***@hotmail.com> 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). constant value in the constructor. Show quoteHide quote > -- > Nathan Sokalski > njsokal...@hotmail.comhttp://www.nathansokalski.com/ > > <joecool1***@live.com> wrote in message > > news:b183820f-8371-46ef-aae0-a2236ad85bd7@n30g2000vba.googlegroups.com... > On Mar 22, 6:39 pm, "Nathan Sokalski" <njsokal***@hotmail.com> 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 > > njsokal...@hotmail.comhttp://www.nathansokalski.com/ > > > "Michel Posseth [MCP]" <M***@posseth.com> wrote in > > messagenews:ua3YsVyqJHA.4***@TK2MSFTNGP04.phx.gbl... > > > > 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" <njsokal***@hotmail.com> schreef in bericht > > >news:%23bk4Y2xqJHA.1504@TK2MSFTNGP03.phx.gbl... > > >>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.
Show quote
Hide quote
"Nathan Sokalski" <njsokal***@hotmail.com> schrieb As this seems to be redundant information, why is "Target" not just a > <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. Readonly property that returns the one ("_blank") or the other (string.empty) value depending on the value of the TargetBlank property? Armin
Other interesting topics
Best practice ASP SQL
how to pass the current user with reportviewer control LoadControl by type Page Panels Drag 'n Drop support for custom asp.net catalogpart/catalogzone. Get argument with imageclickeventhandler. Control is not showing inherited properties in property grid Programmatically add textbox control with incrementing id Is here a web control to create tasks/calendars in Outlook ? Treeview Databinding |
|||||||||||||||||||||||