PC Review


Reply
Thread Tools Rate Thread

CollectionBase inheritance problem

 
 
Jeremy
Guest
Posts: n/a
 
      2nd Mar 2004
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.
 
Reply With Quote
 
 
 
 
Brian Davis
Guest
Posts: n/a
 
      2nd Mar 2004

Inherit from "CollectionBase" instead of "BaseCollection".


Brian Davis
www.knowdotnet.com



"Jeremy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.



 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      3rd Mar 2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.



 
Reply With Quote
 
Jeremy
Guest
Posts: n/a
 
      3rd Mar 2004
Thank you. I hope I'm not the only one who made this dumb mistake...
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem CurrencyManager Collectionbase John Microsoft VB .NET 0 2nd Feb 2007 01:21 PM
CollectionBase problem =?Utf-8?B?Sm9zZW1h?= Microsoft C# .NET 2 4th Mar 2005 05:37 PM
Problem with CollectionBase.List Developer Microsoft Dot NET Framework 5 17th Nov 2004 02:55 AM
Re: Inheritance Problem Abhijeet Dev Microsoft ASP .NET 2 19th Mar 2004 12:41 AM
Re: Inheritance Problem Jay B. Harlow [MVP - Outlook] Microsoft VB .NET 2 29th Aug 2003 02:05 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:51 PM.