469,322 Members | 1,575 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,322 developers. It's quick & easy.

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

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

Jul 20 '06 #1
2 2004
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

Jul 20 '06 #2
It does. I noticed so after I made the post. I must have been doing
something different before.

Kerry Moorman wrote:
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
Jul 20 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Eric Johannsen | last post: by
5 posts views Thread by Hoang Do | last post: by
2 posts views Thread by maxim | last post: by
1 post views Thread by BB | last post: by
1 post views Thread by Ian Ashworth | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.