Default value of a property

Nishithraj
Posted by Nishithraj under Others category on | Views : 1924
We can fix the default value of a property through the code given below.

It will be very useful to avoid the null exceptions while updating to the database. For example if there is a field Acive in data base and it is a notnull field. What will happen if there is no value passed for the property Active from the dotnet part.

We can avoid this by using the Default value attribute as follows.

Private MyVar as Boolean = False
<DefaultValue(False)> _
Public Property Active() As Boolean
Get
Return MyVar
End Get
Set
MyVar = Value
End Set
End Property



private bool myVal=false;

[DefaultValue(false)]
public bool Active {
get {
return myVal;
}
set {
myVal=value;
}
}

Comments or Responses

Login to post response