472,135 Members | 1,162 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,135 software developers and data experts.

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 2088
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
reply views Thread by leo001 | last post: by

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.