467,146 Members | 1,313 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,146 developers. It's quick & easy.

Property problem. Please, need some help on this.

Hello,

I created a user control (.ascx) with a property as follows:

Private _Messages As Generic.List(Of String)
Public Property Messages() As Generic.List(Of String)
Get
Return _Messages
End Get
Set(ByVal value As Generic.List(Of String))
_Messages = value
End Set
End Property ' Messages

I then use the following code in MyPage.aspx.vb where I use this
control:

' MyUserControl_Init
Private Sub MyUserControl_Init(ByVal sender As Object, ByVal e As
EventArgs) Handles MyUserControl.Init

With MyUserControl.Messages
.Add("Message 01")
.Add("Message 02")
End With

End Sub ' MyUserControl_Init

I get the error:

Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.

Any idea why?

I really don't know what I am doing wrong.

Thanks,

Miguel

Feb 25 '07 #1
  • viewed: 980
Share:
1 Reply
Hi there,

You're exposing a reference to the list that has not been instantiated. For
this type of property (internal instance of a list) you must create an
instance and expose it through readonly property, otherwise you allow callers
to change instance whilst you want to change list content only (add or remove
items)

Private _Messages As Generic.List(Of String) = new Generic.List(Of String)()
Public Readonly Property Messages() As Generic.List(Of String)
Get
Return _Messages
End Get
End Property
or "lazy" initialization:

Private _Messages As Generic.List(Of String) = nothing
Public Readonly Property Messages() As Generic.List(Of String)
Get
if _Messages is nothing then
_Messages = new Generic.List(Of String)()
end if
Return _Messages
End Get

Hope this helps

--
Milosz
"shapper" wrote:
Hello,

I created a user control (.ascx) with a property as follows:

Private _Messages As Generic.List(Of String)
Public Property Messages() As Generic.List(Of String)
Get
Return _Messages
End Get
Set(ByVal value As Generic.List(Of String))
_Messages = value
End Set
End Property ' Messages

I then use the following code in MyPage.aspx.vb where I use this
control:

' MyUserControl_Init
Private Sub MyUserControl_Init(ByVal sender As Object, ByVal e As
EventArgs) Handles MyUserControl.Init

With MyUserControl.Messages
.Add("Message 01")
.Add("Message 02")
End With

End Sub ' MyUserControl_Init

I get the error:

Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.

Any idea why?

I really don't know what I am doing wrong.

Thanks,

Miguel

Feb 25 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Rhy Mednick | last post: by
5 posts views Thread by Eric A. Johnson | last post: by
3 posts views Thread by jarnie@gmail.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.