Home All Groups Group Topic Archive Search About

Expose Count from System.Collections.CollectionBase in an inherited class

Author
20 Jul 2006 4:32 PM
GoCoogs
I'm trying to count how many items are in a dynamic collection. This is
the
code I have so far.

*** Begin Code ***
Public Class Rule
    Private _rulevars As RuleVarsCollection
    Private _rulename As String
    Public Property Name() As String
        Get
            Return _rulename
        End Get
        Set(ByVal value As String)
            _rulename = value
        End Set
    End Property
    Public Property VariablesList() As RuleVarsCollection
        Get
            Return _rulevars
        End Get
        Set(ByVal value As RuleVarsCollection)
            _rulevars = value
        End Set
    End Property
    Public Overrides Function ToString() As String
        Return Name.ToString
    End Function
    Public Sub New(ByVal RuleName As String)
        'initialize local variables
        _rulevars = New RuleVarsCollection()
        _rulename = RuleName
    End Sub
End Class
Public Class RuleVar
    'define the Variable class here
    Private _name As String
    Public Property Name() As String
         Get
             Return _name
         End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property
End Class
Public Class RuleVarsCollection
    Inherits System.Collections.CollectionBase
    Public Sub Add(ByVal Var1 As RuleVar)
        List.Add(Var1)
    End Sub
    Public Sub Remove(ByVal index As Integer)
    'Check to see if there is an item at the supplied index.
        If index > Count - 1 Or index < 0 Then
            System.Windows.Forms.MessageBox.Show("Index not Valid!")
        Else
            List.RemoveAt(index)
        End If
    End Sub
    Default Public ReadOnly Property item(ByVal index As Integer) As
RuleVar
        Get
            Return CType(List.Item(index), RuleVar)
        End Get
    End Property
End Class
*** End Code ***

I want to display how many objects are in the collection when it is
selected
from a listbox

*** Begin Code ***
Private Sub ListBox2_SelectedIndexChanged(ByVal sender As
System.Object,
ByVal e As System.EventArgs) Handles ListBox2.SelectedIndexChanged
    Dim r1 As Rule
    r1 = Me.ListBox2.SelectedItem
    MsgBox("Number of items in collection: " & CStr(r1.Count))
End Sub
*** End Code ***

I've been told that I need to expose Count in the inherited class, but
I
don't know how or where to do that. I'm very green.

Thanks,
Blake

Author
20 Jul 2006 5:08 PM
Kerry Moorman
Blake,

Rule is a base class, not an inherited class. What are you trying to count
when you say r1.Count?

Classes that inherit from CollectionBase already have a Count property, so
there is no need to expose it.

Are you trying to get r1.VariablesList.Count? That should work "out of the
box".

Kerry Moorman




Show quoteHide quote
"GoCoogs" wrote:

> I'm trying to count how many items are in a dynamic collection. This is
> the
> code I have so far.
>
> *** Begin Code ***
> Public Class Rule
>     Private _rulevars As RuleVarsCollection
>     Private _rulename As String
>     Public Property Name() As String
>         Get
>             Return _rulename
>         End Get
>         Set(ByVal value As String)
>             _rulename = value
>         End Set
>     End Property
>     Public Property VariablesList() As RuleVarsCollection
>         Get
>             Return _rulevars
>         End Get
>         Set(ByVal value As RuleVarsCollection)
>             _rulevars = value
>         End Set
>     End Property
>     Public Overrides Function ToString() As String
>         Return Name.ToString
>     End Function
>     Public Sub New(ByVal RuleName As String)
>         'initialize local variables
>         _rulevars = New RuleVarsCollection()
>         _rulename = RuleName
>     End Sub
> End Class
> Public Class RuleVar
>     'define the Variable class here
>     Private _name As String
>     Public Property Name() As String
>          Get
>              Return _name
>          End Get
>         Set(ByVal value As String)
>             _name = value
>         End Set
>     End Property
> End Class
> Public Class RuleVarsCollection
>     Inherits System.Collections.CollectionBase
>     Public Sub Add(ByVal Var1 As RuleVar)
>         List.Add(Var1)
>     End Sub
>     Public Sub Remove(ByVal index As Integer)
>     'Check to see if there is an item at the supplied index.
>         If index > Count - 1 Or index < 0 Then
>             System.Windows.Forms.MessageBox.Show("Index not Valid!")
>         Else
>             List.RemoveAt(index)
>         End If
>     End Sub
>     Default Public ReadOnly Property item(ByVal index As Integer) As
> RuleVar
>         Get
>             Return CType(List.Item(index), RuleVar)
>         End Get
>     End Property
> End Class
> *** End Code ***
>
> I want to display how many objects are in the collection when it is
> selected
> from a listbox
>
> *** Begin Code ***
> Private Sub ListBox2_SelectedIndexChanged(ByVal sender As
> System.Object,
> ByVal e As System.EventArgs) Handles ListBox2.SelectedIndexChanged
>     Dim r1 As Rule
>     r1 = Me.ListBox2.SelectedItem
>     MsgBox("Number of items in collection: " & CStr(r1.Count))
> End Sub
> *** End Code ***
>
> I've been told that I need to expose Count in the inherited class, but
> I
> don't know how or where to do that. I'm very green.
>
> Thanks,
> Blake
>
>
Author
20 Jul 2006 6:50 PM
GoCoogs
It does. I noticed so after I made the post. I must have been doing
something different before.

Kerry Moorman wrote:
Show quoteHide quote
> Blake,
>
> Rule is a base class, not an inherited class. What are you trying to count
> when you say r1.Count?
>
> Classes that inherit from CollectionBase already have a Count property, so
> there is no need to expose it.
>
> Are you trying to get r1.VariablesList.Count? That should work "out of the
> box".
>
> Kerry Moorman
>
>
>
>
> "GoCoogs" wrote:
>
> > I'm trying to count how many items are in a dynamic collection. This is
> > the
> > code I have so far.
> >
> > *** Begin Code ***
> > Public Class Rule
> >     Private _rulevars As RuleVarsCollection
> >     Private _rulename As String
> >     Public Property Name() As String
> >         Get
> >             Return _rulename
> >         End Get
> >         Set(ByVal value As String)
> >             _rulename = value
> >         End Set
> >     End Property
> >     Public Property VariablesList() As RuleVarsCollection
> >         Get
> >             Return _rulevars
> >         End Get
> >         Set(ByVal value As RuleVarsCollection)
> >             _rulevars = value
> >         End Set
> >     End Property
> >     Public Overrides Function ToString() As String
> >         Return Name.ToString
> >     End Function
> >     Public Sub New(ByVal RuleName As String)
> >         'initialize local variables
> >         _rulevars = New RuleVarsCollection()
> >         _rulename = RuleName
> >     End Sub
> > End Class
> > Public Class RuleVar
> >     'define the Variable class here
> >     Private _name As String
> >     Public Property Name() As String
> >          Get
> >              Return _name
> >          End Get
> >         Set(ByVal value As String)
> >             _name = value
> >         End Set
> >     End Property
> > End Class
> > Public Class RuleVarsCollection
> >     Inherits System.Collections.CollectionBase
> >     Public Sub Add(ByVal Var1 As RuleVar)
> >         List.Add(Var1)
> >     End Sub
> >     Public Sub Remove(ByVal index As Integer)
> >     'Check to see if there is an item at the supplied index.
> >         If index > Count - 1 Or index < 0 Then
> >             System.Windows.Forms.MessageBox.Show("Index not Valid!")
> >         Else
> >             List.RemoveAt(index)
> >         End If
> >     End Sub
> >     Default Public ReadOnly Property item(ByVal index As Integer) As
> > RuleVar
> >         Get
> >             Return CType(List.Item(index), RuleVar)
> >         End Get
> >     End Property
> > End Class
> > *** End Code ***
> >
> > I want to display how many objects are in the collection when it is
> > selected
> > from a listbox
> >
> > *** Begin Code ***
> > Private Sub ListBox2_SelectedIndexChanged(ByVal sender As
> > System.Object,
> > ByVal e As System.EventArgs) Handles ListBox2.SelectedIndexChanged
> >     Dim r1 As Rule
> >     r1 = Me.ListBox2.SelectedItem
> >     MsgBox("Number of items in collection: " & CStr(r1.Count))
> > End Sub
> > *** End Code ***
> >
> > I've been told that I need to expose Count in the inherited class, but
> > I
> > don't know how or where to do that. I'm very green.
> >
> > Thanks,
> > Blake
> >
> >