Property. Which one should I use?

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

Is there a difference between defining a control property in the
following two ways:

' Items ...
Private _Items As Generic.List(Of ListItem) = New Generic.List(Of
ListItem) ****
Property Items() As Generic.List(Of ListItem)
Get
Return _Items
End Get
Set(ByVal value As Generic.List(Of ListItem))
_Items = value
End Set
End Property ' Items

Or

' Items ...
Private _Items As Generic.List(Of ListItem) ****
Property Items() As Generic.List(Of ListItem)
Get
If _Items Is Nothing Then
****
_Items = New Generic.List(Of WebControl) ****
End
If ****
Return _Items
End Get
Set(ByVal value As Generic.List(Of ListItem))
_Items = value
End Set
End Property ' Items

I marked the differences with ****.

Which approach should I use?

Thanks,
Miguel
 
Why declare it as ListItem, then assign as WebControl?

Sorry,

That was a type mistake.
My question is if I should declare a default value when I do "dim ..."

Or use an IF inside Get in property.

Thanks,
Miguel
 
Miguel,

At the very least, you should declare it and assign as Nothing.

In the property, you can assign the List to have a value if it's still
Nothing. That is fine; it's also known as "Lazy Instantiation"
 

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. Control or View State? 1
Property. 3
Control / Property. Going crazy here. 1
CheckBox 1
Property problem. Please, need some help on this. 1
Is Nothing problem 16
Generic List. Remove duplicate 2
String 2

Back
Top