[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Nathan Sokalski

3/22/2009 6:40:00 PM

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
njsokalski@hotmail.com
http://www.nathansok...


9 Answers

unknown

3/22/2009 7:02:00 PM

0

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.

Michel Posseth [MCP]

3/22/2009 7:37:00 PM

0

Nathan

This is a verry good article to read for you

http://www.dotnetarticles.net/Graphics-Programmin...

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" <njsokalski@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
> njsokalski@hotmail.com
> http://www.nathansok...
>


James Hahn

3/22/2009 9:38:00 PM

0

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]" <MSDN@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-Programmin...
>
> 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" <njsokalski@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
>> njsokalski@hotmail.com
>> http://www.nathansok...
>>
>
>

Nathan Sokalski

3/22/2009 10:40:00 PM

0

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
njsokalski@hotmail.com
http://www.nathansok...

"Michel Posseth [MCP]" <MSDN@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-Programmin...
>
> 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" <njsokalski@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
>> njsokalski@hotmail.com
>> http://www.nathansok...
>>
>
>


Joe Cool

3/22/2009 11:52:00 PM

0

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.nathansok...
>
> "Michel Posseth [MCP]" <M...@posseth.com> wrote in messagenews:ua3YsVyqJHA.4996@TK2MSFTNGP04.phx.gbl...
>
>
>
> > Nathan
>
> > This is a verry good article to read for you
>
> >http://www.dotnetarticles.net/Graphics-Programmin...
>
> > 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.

Nathan Sokalski

3/23/2009 12:19:00 AM

0

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
njsokalski@hotmail.com
http://www.nathansok...

<joecool1969@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.nathansok...
>
> "Michel Posseth [MCP]" <M...@posseth.com> wrote in
> messagenews:ua3YsVyqJHA.4996@TK2MSFTNGP04.phx.gbl...
>
>
>
> > Nathan
>
> > This is a verry good article to read for you
>
> >http://www.dotnetarticles.net/Graphics-Programmin...
>
> > 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.


Joe Cool

3/23/2009 12:41:00 AM

0

On 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).

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

> --
> Nathan Sokalski
> njsokal...@hotmail.comhttp://www.nathansok...
>
> <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.nathansok...
>
> > "Michel Posseth [MCP]" <M...@posseth.com> wrote in
> > messagenews:ua3YsVyqJHA.4996@TK2MSFTNGP04.phx.gbl...
>
> > > Nathan
>
> > > This is a verry good article to read for you
>
> > >http://www.dotnetarticles.net/Graphics-Programmin...
>
> > > 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.

Nathan Sokalski

3/23/2009 2:00:00 AM

0

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
njsokalski@hotmail.com
http://www.nathansok...

<joecool1969@live.com> wrote in message
news:6b50e872-a77a-4cba-bc3a-019f8d05fb75@e5g2000vbe.googlegroups.com...
On 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).

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

> --
> Nathan Sokalski
> njsokal...@hotmail.comhttp://www.nathansok...
>
> <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.nathansok...
>
> > "Michel Posseth [MCP]" <M...@posseth.com> wrote in
> > messagenews:ua3YsVyqJHA.4996@TK2MSFTNGP04.phx.gbl...
>
> > > Nathan
>
> > > This is a verry good article to read for you
>
> > >http://www.dotnetarticles.net/Graphics-Programmin...
>
> > > 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.


Armin Zingler

3/23/2009 2:20:00 AM

0

"Nathan Sokalski" <njsokalski@hotmail.com> 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