473,770 Members | 4,198 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_I nit
Private Sub MyUserControl_I nit(ByVal sender As Object, ByVal e As
EventArgs) Handles MyUserControl.I nit

With MyUserControl.M essages
.Add("Message 01")
.Add("Message 02")
End With

End Sub ' MyUserControl_I nit

I get the error:

Exception Details: System.NullRefe renceException: 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 1132
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_I nit
Private Sub MyUserControl_I nit(ByVal sender As Object, ByVal e As
EventArgs) Handles MyUserControl.I nit

With MyUserControl.M essages
.Add("Message 01")
.Add("Message 02")
End With

End Sub ' MyUserControl_I nit

I get the error:

Exception Details: System.NullRefe renceException: 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
6274
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 Sub). Ideas? Sub SetStartupProperties() On Error GoTo SetStartupProperties_Err 10 ChangeProperty "StartupForm", dbText, "frmLaunch" ' Form you want to open at startup 20 ChangeProperty "StartupShowDBWindow", dbBoolean, False ...
0
3304
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, but I feel compelled to mention it because it can be hard for someone to imagine my challenge when I talk in hypotheticals. Anyway, these controls each have an Ink property. For non-Tablet developers, Ink is a class defined in the Tablet PC...
7
3018
by: Shimon Sim | last post by:
I have a custom composite control I have following property
5
9165
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 comboQueryTitle_SelectedIndexChanged handler to, when a new selection is made, update the query and refresh the display. The only problem is that the program seems to inexplicably start with the ..selectedindex property at -1! This makes no sense to me. ...
3
1770
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 After a few different approaches, I have ended up overriding the OnPaint method and using Graphics.CopyFromScreen and then painting the transparent PNG on top of this using DrawImage. I have so far been able to get the same effect when stationary, but...
0
9592
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10059
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10005
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7416
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5452
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3972
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2817
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.