Property.

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

shapper

Hello,

On my profile I have a property as follows:

Private _Levels As Generic.List(Of Enumeration.Level)
Public Property Levels() As Generic.List(Of Enumeration.Level)
Get
If _Levels Is Nothing Then
Return New Generic.List(Of Enumeration.Level)
End If
Return _Levels
End Get
Set(ByVal value As Generic.List(Of Enumeration.Level))
_Levels = value
End Set
End Property ' Levels

When I create a new profile I don't add any items to this property ...
only later.
However when later I try to add items they are not added.

The only way to make this work is to, when I create the profile for
the new user, the following:
Profile.Levels = New Generic.List(Of Level)

Just like an initialization.

Well, do I really need to do that?
Maybe my property is not completely right ...

Should I change something in my property?

Thanks,
Miguel
 
shapper,

you need to correct your code to make it work:
Get
If _Levels Is Nothing Then
' in the next line change Return to _Levels =
_Levels = New Generic.List(Of Enumeration.Level)
End If
Return _Levels
End Get
 
shapper said:
Hello,

On my profile I have a property as follows:

Private _Levels As Generic.List(Of Enumeration.Level)
Public Property Levels() As Generic.List(Of Enumeration.Level)
Get
If _Levels Is Nothing Then
Return New Generic.List(Of Enumeration.Level)
End If
Return _Levels
End Get
Set(ByVal value As Generic.List(Of Enumeration.Level))
_Levels = value
End Set
End Property ' Levels

When I create a new profile I don't add any items to this property ...
only later.
However when later I try to add items they are not added.

Why do you think that they are not added? What have you done to come to
this conclusion?
 
Sergey said:
shapper,

you need to correct your code to make it work:
Get
If _Levels Is Nothing Then
' in the next line change Return to _Levels =
_Levels = New Generic.List(Of Enumeration.Level)
End If
Return _Levels
End Get

That doesn't have anything to do with the problem. It makes the property
return the same empty list instead of creating a new one every time if
the property is read multiple times before it's set, so it's not a bad
thing to do, but it's not relevant for the question.
 
Back
Top