Specifying property defaults

N

Nathan Sokalski

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.
 
B

bruce barker

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)
 
J

James Hahn

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")
 
N

Nathan Sokalski

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.
 
J

joecool1969

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.
 
N

Nathan Sokalski

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
(e-mail address removed)
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.
--
Nathan Sokalski
(e-mail address removed)://www.nathansokalski.com/

message

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.
 
J

joecool1969

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.
 
N

Nathan Sokalski

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
(e-mail address removed)
http://www.nathansokalski.com/

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.
 
A

Armin Zingler

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

Similar Threads

Simple, but difficult to me. Image border property 1
Control Property. 3
Property 0
Property 1
ConfigParser.get() defaults? 7
Property 1
Property 1
Arduino Code Please Help 0

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top