Which is the best way to set default value for properties
-
Hi there, I'm newbie here, I want to know, to set default value for property, which is the best way? 1.
Private _AddFoto As Boolean = False
Public Property AddFoto() As Boolean
Get
Return _AddFoto
End Get
Set(ByVal value As Boolean)
_AddFoto = value
End Set
End PropertyOr 2.
Private _AddFoto As Boolean
<ComponentModel.DefaultValue(False)> _
Public Property AddFoto() As Boolean
Get
Return _AddFoto
End Get
Set(ByVal value As Boolean)
_AddFoto = value
End Set
End PropertyThanks
-
Hi there, I'm newbie here, I want to know, to set default value for property, which is the best way? 1.
Private _AddFoto As Boolean = False
Public Property AddFoto() As Boolean
Get
Return _AddFoto
End Get
Set(ByVal value As Boolean)
_AddFoto = value
End Set
End PropertyOr 2.
Private _AddFoto As Boolean
<ComponentModel.DefaultValue(False)> _
Public Property AddFoto() As Boolean
Get
Return _AddFoto
End Get
Set(ByVal value As Boolean)
_AddFoto = value
End Set
End PropertyThanks
I always use the method of your first example.
Steve Jowett ------------------------- Real programmers don't comment their code. If it was hard to write, it should be hard to read.
-
Hi there, I'm newbie here, I want to know, to set default value for property, which is the best way? 1.
Private _AddFoto As Boolean = False
Public Property AddFoto() As Boolean
Get
Return _AddFoto
End Get
Set(ByVal value As Boolean)
_AddFoto = value
End Set
End PropertyOr 2.
Private _AddFoto As Boolean
<ComponentModel.DefaultValue(False)> _
Public Property AddFoto() As Boolean
Get
Return _AddFoto
End Get
Set(ByVal value As Boolean)
_AddFoto = value
End Set
End PropertyThanks
-
I always use the first case. Does case 2 work? I tried your sample by setting
<Componentmodel.Defaultvalue(True)>...
but this doesn't seem to work.
Tosch
I used the second code, but i didn't realize until your reply. Yes, the 2nd code doesnt work if we change the value to 'true' I googling to msdn and find this statement: "A DefaultValueAttribute will not cause a member to be automatically initialized with the attribute's value. You must set the initial value in your code."
Private MyVar as Boolean = False
<DefaultValue(False)> _
Public Property MyProperty() As Boolean
Get
Return MyVar
End Get
Set
MyVar = Value
End Set
End Propertyand it make me more confuse,what is the function of this <DefaultValue(False)>
-
Hi there, I'm newbie here, I want to know, to set default value for property, which is the best way? 1.
Private _AddFoto As Boolean = False
Public Property AddFoto() As Boolean
Get
Return _AddFoto
End Get
Set(ByVal value As Boolean)
_AddFoto = value
End Set
End PropertyOr 2.
Private _AddFoto As Boolean
<ComponentModel.DefaultValue(False)> _
Public Property AddFoto() As Boolean
Get
Return _AddFoto
End Get
Set(ByVal value As Boolean)
_AddFoto = value
End Set
End PropertyThanks
Apparently, i missunderstanding what i read before. componenmodel.defaultvalue is used for other purpose (designer). thanks
-
Hi there, I'm newbie here, I want to know, to set default value for property, which is the best way? 1.
Private _AddFoto As Boolean = False
Public Property AddFoto() As Boolean
Get
Return _AddFoto
End Get
Set(ByVal value As Boolean)
_AddFoto = value
End Set
End PropertyOr 2.
Private _AddFoto As Boolean
<ComponentModel.DefaultValue(False)> _
Public Property AddFoto() As Boolean
Get
Return _AddFoto
End Get
Set(ByVal value As Boolean)
_AddFoto = value
End Set
End PropertyThanks
You have the other responses also, but now there is the new VS2010 way; Public Property theProperty as boolean = false Thats it, all the other Get, Set and local private variable are all taken care of under the hood. [which you can still use if you want, or depending on scenario] The local private variable will be named _theProperty by default, and accessible by that name.
Dave Don't forget to rate messages!
Find Me On: Web|Facebook|Twitter|LinkedIn
Waving? dave.m.auld[at]googlewave.com