473,396 Members | 1,755 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,396 software developers and data experts.

CollectionBase inheritance problem

I am creating a custom collection. Very simple.

Imports System.Collections

Public Class ShapesCollection
Inherits BaseCollection

Default Public Property Item(ByVal index As Integer) As Shape
Get
Return CType(List(index), Shape)
End Get
Set(ByVal Value As Shape)
List(index) = Value
End Set
End Property

Public Function Add(ByVal value As Shape) As Integer
MsgBox(value Is Nothing)
MsgBox(list Is Nothing)

Return List.Add(value)
End Function

End Class

My client code looks like this:

Public Class frmDraw
Inherits System.Windows.Forms.Form

Dim m_Shapes As ShapesCollection
Dim rand As New Random
Dim m_PanelGraphics As Graphics

....code snipped for brevity

Private Sub frmDraw_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
cboColors.Text = "Black"
m_Shapes = New ShapesCollection
m_PanelGraphics = Panel1.CreateGraphics
End Sub

Private Sub cmdCircle_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cmdCircle.Click

m_Shapes.Add(New Circle(rand.Next(50, 200), rand.Next(10,
300), rand.Next(10, 50), Color.FromName(cboColors.Text)))
RefreshScreen()

End Sub

....code snipped for brevity

End Class

When I run this, I get a NullReferenceException in the Add method of
my custom collection. The first msgbox pops up False, and the Second
pops up
True. This means the List property of the BaseCollection class is
Nothing.
How can this be??? I would think the base class would be smart enough
to
initialize its own variables.
Nov 20 '05 #1
3 1424

Inherit from "CollectionBase" instead of "BaseCollection".
Brian Davis
www.knowdotnet.com

"Jeremy" <jm**********@yahoo.com> wrote in message
news:11**************************@posting.google.c om...
I am creating a custom collection. Very simple.

Imports System.Collections

Public Class ShapesCollection
Inherits BaseCollection

Default Public Property Item(ByVal index As Integer) As Shape
Get
Return CType(List(index), Shape)
End Get
Set(ByVal Value As Shape)
List(index) = Value
End Set
End Property

Public Function Add(ByVal value As Shape) As Integer
MsgBox(value Is Nothing)
MsgBox(list Is Nothing)

Return List.Add(value)
End Function

End Class

My client code looks like this:

Public Class frmDraw
Inherits System.Windows.Forms.Form

Dim m_Shapes As ShapesCollection
Dim rand As New Random
Dim m_PanelGraphics As Graphics

...code snipped for brevity

Private Sub frmDraw_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
cboColors.Text = "Black"
m_Shapes = New ShapesCollection
m_PanelGraphics = Panel1.CreateGraphics
End Sub

Private Sub cmdCircle_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cmdCircle.Click

m_Shapes.Add(New Circle(rand.Next(50, 200), rand.Next(10,
300), rand.Next(10, 50), Color.FromName(cboColors.Text)))
RefreshScreen()

End Sub

...code snipped for brevity

End Class

When I run this, I get a NullReferenceException in the Add method of
my custom collection. The first msgbox pops up False, and the Second
pops up
True. This means the List property of the BaseCollection class is
Nothing.
How can this be??? I would think the base class would be smart enough
to
initialize its own variables.

Nov 20 '05 #2
Jeremy,
As Brian suggested, Inherit from System.Collection.CollectionBase.

It appears that you inadvertently inherited from
System.Windows.Forms.BaseCollection.

The BaseCollection.List property is overridable requiring you to override it
and supply an ArrayList.

The CollectionBase.List property is not overridable, as CollectionBase
supplies & maintains the ArrayList for you.

Hope this helps
Jay

"Jeremy" <jm**********@yahoo.com> wrote in message
news:11**************************@posting.google.c om...
I am creating a custom collection. Very simple.

Imports System.Collections

Public Class ShapesCollection
Inherits BaseCollection

Default Public Property Item(ByVal index As Integer) As Shape
Get
Return CType(List(index), Shape)
End Get
Set(ByVal Value As Shape)
List(index) = Value
End Set
End Property

Public Function Add(ByVal value As Shape) As Integer
MsgBox(value Is Nothing)
MsgBox(list Is Nothing)

Return List.Add(value)
End Function

End Class

My client code looks like this:

Public Class frmDraw
Inherits System.Windows.Forms.Form

Dim m_Shapes As ShapesCollection
Dim rand As New Random
Dim m_PanelGraphics As Graphics

...code snipped for brevity

Private Sub frmDraw_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
cboColors.Text = "Black"
m_Shapes = New ShapesCollection
m_PanelGraphics = Panel1.CreateGraphics
End Sub

Private Sub cmdCircle_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cmdCircle.Click

m_Shapes.Add(New Circle(rand.Next(50, 200), rand.Next(10,
300), rand.Next(10, 50), Color.FromName(cboColors.Text)))
RefreshScreen()

End Sub

...code snipped for brevity

End Class

When I run this, I get a NullReferenceException in the Add method of
my custom collection. The first msgbox pops up False, and the Second
pops up
True. This means the List property of the BaseCollection class is
Nothing.
How can this be??? I would think the base class would be smart enough
to
initialize its own variables.

Nov 20 '05 #3
Thank you. I hope I'm not the only one who made this dumb mistake...
Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Steve M | last post by:
I have subclassed CollectionBase. I have also implemented GetEnumerator(). I have tried to set the DataSource of a DataGrid to an instance of my subclass. However, the items in the grid are not...
7
by: m. pollack | last post by:
Hi all, I've been using the CollectionBase class to implement a strongly-typed collection, but I have noticed that the RemoveAt method does not seem to call the "On" hook methods (OnRemove,...
2
by: m.pollack | last post by:
Hi all, I have an application which uses a class object that contains a collection. In order to use the PropertyGrid control to expose properties to the user at runtime, I created a...
5
by: Eric Johannsen | last post by:
I have a simple object that inherits from CollectionBase and overrides the Count property: namespace MyTest { public class CollTest : System.Collections.CollectionBase { public override int...
1
by: Morten Dyndgaard | last post by:
Hi, I want to do the following: I have to XmlSerialize a HashTable, but since it inherits from IDictionary it cannot be XmlSerialized, and I prefer not to make my own implementation of...
1
by: Matthew Roberts | last post by:
Howdy Everyone, I am having trouble understanding the process of creating a type-safe collection by inheriting from the CollectionBase class. I have done it plenty of times, but now that I sit...
2
by: Samuel R. Neff | last post by:
What's the advantage of inheriting from CollectionBase as opposed to just implementing IList? It seems that it saves you from having to implement a few properties (Clear, CopyTo, Count,...
0
by: LIJO CHEERAN | last post by:
Hello friends I am trying to study about CollectionBase. I have inherited CollectionBase in the class TheCollection.cs. I am using the “TheCollection. in an aspx page to store objects...
3
by: Tony Johansson | last post by:
Hello! Sorry for opening up this task again. I want to fully understand this List that is return from CollectionBase. According to you is List in CollectionBase implemented something like...
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: 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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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
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...

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.