Default property value

S

shapper

Hello,

I am creating a class where I have various properties.
How to I set a default property value in case the property is not
defined by the user.

For example, I have the property:

' Priority
Public Property Priority() As Mail.MailPriority
Get
Return _Priority
End Get
Set(ByVal value As Mail.MailPriority)
_Priority = value
End Set
End Property

I want to set it to Mail.MailPriority.Normal in case the user didn't
defined it.

How can I do this?

Thanks,
Miguel
 
A

apathetic

How to I set a default property value in case the property is not
defined by the user.

You are storing the actual value of the property in the _Priority
field, so you would just set an initial value for this variable when
it's declared:

Dim _Priority As Mail.MailPriority = Mail.MailPriority.Normal

(I think this is the VB syntax, I'm a bit rusty.)

Tim
 
R

Robert Haken [MVP]

I am creating a class where I have various properties.
How to I set a default property value in case the property is not
defined by the user.

For example, I have the property:

' Priority
Public Property Priority() As Mail.MailPriority
Get
Return _Priority
End Get
Set(ByVal value As Mail.MailPriority)
_Priority = value
End Set
End Property

I want to set it to Mail.MailPriority.Normal in case the user didn't
defined it.

How can I do this?

Basically, there are two techniques with lots of variations, early
initialization and late initialization:

Early initialization initializes the value when it is declared, or in
constructor:

private Mail.MailPriority _priority = Mail.MailPriority.Normal;
-or-
public Class()
{
_priority = Mail.MailPriority.Normal;
}

Late initialization initializes the value within first use:

public Mail.MailPriority Priority
{
get
{
if (_priority == null)
{
_priority = Mail.MailPriority.Normal;
}
return _priority;
}
...
}

Robert Haken [MVP ASP/ASP.NET]
HAVIT, s.r.o., www.havit.cz
http://knowledge-base.havit.cz
 
M

Morten Wennevik

You could also use the DefaultValueAttribute. The effect would be the
same.

<DefaultValue(Mail.MailPriority.Normal)>
Public Property Priority() As Mail.MailPriority
Get
Return _Priority
End Get
Set(ByVal value As Mail.MailPriority)
_Priority = value
End Set
End Property
 
S

shapper

Hi Morten,

That was exactly what I was trying but I wasn't able to make it work.
Now I did made it work using:

<DefaultValue(Mail.MailPriority.Normal)> _
Public Property Priority() As Mail.MailPriority

I added the '_' at the end of DefaultValue code line.

Thanks,
Miguel
 

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

Property default value 1
Property Default Value 1
Property 1
Custom Class 3
Control Property. 3
Check property value 1
Default value 1
Simple, but difficult to me. Image border property 1

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top