Overriding Item in Inherited Arraylist

M

Mykl

I'm trying to derive a class from ArrayList. When I try to use the
Overrides keyword on the Item property, the IDE underlines Item in my
Function definition and says "Item conflicts with a property by the same
name declared in ArrayList"... what gives? Am I out to lunch? I should
add that I'm still a little new to OO and inheritance, polymorphism, etc,
and would really like to understand this...

Here's my code:

Public Class NickNamesCollection
Inherits ArrayList

Public Overloads Function Add(ByVal subjectName As String) As String
MyBase.Add(subjectName)
End Function

' This is where the error occurs
Public Overrides Function Item(ByVal idx As Integer) As String
If idx < MyBase.Count Then
Return CType(MyBase.Item(idx), String)
Else
Throw New Exception("The subject has only " &
MyBase.Count.ToString & " nick-names")
End If
End Function

Public Function GetString() As String
Dim i As Integer
Dim str As String
For i = 0 To MyBase.Count - 1
str = str & MyBase.Item(i).ToString & vbCrLf
Next
Return str
End Function

End Class


Thanks,
Michael
 
A

Armin Zingler

Mykl said:
I'm trying to derive a class from ArrayList. When I try to use the
Overrides keyword on the Item property, the IDE underlines Item in
my
Function definition and says "Item conflicts with a property by the
same name declared in ArrayList"... what gives? Am I out to lunch?
I should add that I'm still a little new to OO and inheritance,
polymorphism, etc,
and would really like to understand this...

Item is a property, not a function.

From object browser:
Public Overridable Default Property Item(ByVal index As Integer) As Object

Armin
 
M

Mykl

Ok, but then, won't I have to write the code to sort?--- that is, I wanted
to use the Sort method from ArrayList.

m
 
J

Jay B. Harlow [MVP - Outlook]

Mykl,
CollectionBase has an InnerList property which is the contained ArrayList.
You can call Sort on this value.

If you want your collection to allowing Sorting, I would simply Delgate to
it.

Something like:

Public Class NickNamesCollection
Inherits CollectionBase

Public Function Add(ByVal subjectName As String) As String
MyBase.InnerList.Add(subjectName)
End Function

Default Public ReadOnly Property Item(ByVal index As Integer) As String
Get
Return DirectCast(MyBase.InnerList.Item(index), String)
End Get
End Property

Public Overrides Function ToString() As String
If Me.Count = 0 Then Return String.Empty
Dim sb As New System.Text.StringBuilder
For Each str As String In MyBase.InnerList
sb.Append(str)
sb.Append(ControlChars.CrLf)
Next
sb.Length -= ControlChars.CrLf.Length
Return sb.ToString()
End Function

Public Sub Sort()
MyBase.InnerList.Sort()
End Sub

Public Sub Sort(ByVal comparer As IComparer)
MyBase.InnerList.Sort(comparer)
End Sub

End Class

Hope this helps
Jay

| Ok, but then, won't I have to write the code to sort?--- that is, I wanted
| to use the Sort method from ArrayList.
|
| m
|
| | > Mykl,
| >
| > I would not try to inherit from arraylist. It is a quit basic class.
| > Therefore when you want something more sophisticated, make it yourself
| > either by doing it from Ilist of from collectionbase.
| >
| >
http://msdn.microsoft.com/library/d...systemcollectionscollectionbaseclasstopic.asp
| >
| > I hope this helps,
| >
| > Cor
| >
|
|
 
M

Mykl

Dynamite! I'll have a close look to try to understand it... oh, and thanks
for cleaning up my String returning function too.

Mykl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top