Tuesday, March 13, 2012

Set property default value

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

You need to use [DefaultValue(datatype)] for your property definition

For example check thisSet Property Default Value

Thanks


I haven't verified/tested but is the [DefaultValue(datatype)] only used by the Property Browser control ?

An other option is the set your variable in your constructor or in the declaration (Dim a As Integer = 1)


Replace

private _Priority As String

with

private _Priority As String = "Normal"

0 comments:

Post a Comment