473,396 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Overriding Item in Inherited Arraylist

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
Nov 21 '05 #1
5 2549
"Mykl" <no**@nope.com> schrieb
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
Nov 21 '05 #2
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/de...classtopic.asp

I hope this helps,

Cor
Nov 21 '05 #3
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

"Cor Ligthert" <no************@planet.nl> wrote in message
news:OF**************@TK2MSFTNGP15.phx.gbl...
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/de...classtopic.asp

I hope this helps,

Cor

Nov 21 '05 #4
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

"Mykl" <no**@nope.com> wrote in message
news:Oz**************@TK2MSFTNGP10.phx.gbl...
| 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
|
| "Cor Ligthert" <no************@planet.nl> wrote in message
| news:OF**************@TK2MSFTNGP15.phx.gbl...
| > 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/de...classtopic.asp
| >
| > I hope this helps,
| >
| > Cor
| >
|
|
Nov 21 '05 #5
Dynamite! I'll have a close look to try to understand it... oh, and thanks
for cleaning up my String returning function too.

Mykl
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:ew**************@tk2msftngp13.phx.gbl...
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

"Mykl" <no**@nope.com> wrote in message
news:Oz**************@TK2MSFTNGP10.phx.gbl...
| 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
|
| "Cor Ligthert" <no************@planet.nl> wrote in message
| news:OF**************@TK2MSFTNGP15.phx.gbl...
| > 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/de...classtopic.asp
| >
| > I hope this helps,
| >
| > Cor
| >
|
|

Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Edward Diener | last post by:
Is it possible for a derived class to override a property and/or event of its base class ?
0
by: Ian | last post by:
I've got this problem with overriding attributes on an inheritance chain where the XmlSerializer is concerned. For example: public class A { private string aWord = String.Empty(); public...
1
by: leal ting | last post by:
a class inherited from ArrayList, is saved to ViewState, why the type of the object read from ViewSate is not the class, but the parent, ArrayList lealting@hotmail.com] the class inherited...
12
by: Rubbrecht Philippe | last post by:
Hi there, According to documentation I read the ArrayList.IndexOf method uses the Object.Equals method to loop through the items in its list and locate the first index of an item that returns...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.