473,394 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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
1 1114
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: MLH | last post by:
I call the following Sub and Function in frmLaunch's OnOpen event code. I keep getting Property Not Found error for the AllowBypassKey setting. Failure point is line #30 in the Function (not the...
0
by: Rhy Mednick | last post by:
I have a series of simple controls that I created to make some aspects of Tablet PC programming a little easier. The "Tablet PC" aspects of the controls are irrelevant to the problem I'm having,...
7
by: Shimon Sim | last post by:
I have a custom composite control I have following property
5
by: Eric A. Johnson | last post by:
Hi Everyone, I am at my wit's end here. I have a combobox (combyQueryTitle) that I need to use in order to select a query for my database project. Therefore, I am using the...
3
by: jarnie | last post by:
I am attempting to make a form which has alpha transparency that varies across the form, similar to the Adobe splash screens and launchy: http://www.launchy.net/images/screenshot_sheep.jpg ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.