Specifying property defaults

Nathan Sokalski

Usenet Archive
Joined
Jul 22, 2003
Messages
1,332
Reaction score
0
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

Usenet Archive
Joined
Mar 29, 2002
Messages
5,092
Reaction score
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)
 

James Hahn

Usenet Archive
Joined
Oct 27, 2008
Messages
4
Reaction score
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")
 

Nathan Sokalski

Usenet Archive
Joined
Jul 22, 2003
Messages
1,332
Reaction score
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.
 

joecool1969

Usenet Archive
Joined
Mar 22, 2009
Messages
2
Reaction score
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.

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

Usenet Archive
Joined
Jul 22, 2003
Messages
1,332
Reaction score
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).
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.

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.
 

joecool1969

Usenet Archive
Joined
Mar 22, 2009
Messages
2
Reaction score
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).

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

Nathan Sokalski

Usenet Archive
Joined
Jul 22, 2003
Messages
1,332
Reaction score
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!
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.
 

Armin Zingler

Usenet Archive
Joined
Apr 26, 2004
Messages
17
Reaction score
0
Nathan Sokalski said:
<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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,051
Messages
3,104,251
Members
248,135
Latest member
tobystanx

Latest Threads

Top