Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ArrayList ToArray issues

0 views
Skip to first unread message

Nak

unread,
Aug 23, 2003, 7:31:02 AM8/23/03
to
Hi there,

Has anyone any idea why I cannot do the following in a class inherited
from ArrayList?

Public Shadows Function toArray() As Rectangle()
Return (MyBase.ToArray())
End Function

I can do this with other objects, but seemingly not objects in the
drawing namespace (I'm sure there must be others), any idea how I get around
this?

Nick.


Mattias Sjögren

unread,
Aug 23, 2003, 8:05:58 AM8/23/03
to
> I can do this with other objects, but seemingly not objects in the
>drawing namespace (I'm sure there must be others), any idea how I get around
>this?

It doesn't work because Rectangle is a value type (Structure) and is
boxed to be stored in an ArrayList. Getting the rectangle back out
requires unboxing.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

Jay B. Harlow [MVP - Outlook]

unread,
Aug 23, 2003, 10:19:21 AM8/23/03
to
Nak,
First, I question why are inheriting from ArrayList, as opposed to
CollectionBase? CollectionBase is a wrapper around ArrayList allowing type
safe collections. If you are shadowing functions (Add & ToArray for
example), then things really won't work as you expect. As I can assign your
class to an ArrayList variable use the normal Add or ToArray; Which will
circumnavigate your Add & ToArray method! To prevent this circumnavigation
you need to use inheritance and Override the method! CollectionBase can be
assigned to an IList variable, however CollectionBase has overridable
OnValidate, OnInsert, OnRemove, & OnSet methods to ensure type safety.
CollectionBase has a handful of other On* methods that you can override for
other notifications.

As Mattias said Rectangle is a value type and would need to be unboxed, the
ArrayList.ToArray does not automatically unbox value types.

I would define a new Rectangle array of the size (ArrayList.Count - 1)
needed than using a for to loop, assign the values from the ArrayList to
this new Array.

Hope this helps
Jay

"Nak" <a...@a.com> wrote in message
news:erSqJoWa...@TK2MSFTNGP12.phx.gbl...

Nak

unread,
Aug 23, 2003, 10:37:33 AM8/23/03
to
Hi Jay

I understand the ToArray method not working now thanks to Mattias, I had
just presumed that it was an Object, but never mind.

The reason I am inheriting from arraylist is because I want features
such as "AddRange" and such like that I cannot get from CollectionBase, I
have tested the collection and it works very well, what are you saying
shouldn't work? It works just the same as my previous named collections
just by using inheritance rather than declaring a private arraylist and
exposing it's methods. Are you saying that I shouldn't be shadowing? I am
shadowing because I wish the class to be strongly named, for example I wish
to add to the collection via it's own type not just object. Maybe I'm doing
it the wrong way but it seems to work perfectly so far, here is an example
of a listviewitem collection of mine, is it wrong?

----------------------------------------------------

Public Class listViewItem_Collection
Inherits ArrayList

#Region "Property interface"

Public Shadows Property item(ByVal iIndex As Integer) As
ListViewItem
Get
Return (MyBase.Item(iIndex))
End Get
Set(ByVal Value As ListViewItem)
MyBase.Item(iIndex) = Value
End Set
End Property

#End Region

#Region "New / Finalize"

Public Sub New()
Call MyBase.New(initialArrayListMaxCapacity)
End Sub

Protected Overrides Sub Finalize()
Call MyBase.Finalize()
End Sub

#End Region

#Region "Public methods"

Public Shadows Function add(ByVal iListViewItem As ListViewItem) As
Integer
Return MyBase.Add(iListViewItem)
End Function

Public Shadows Sub addRange(ByVal iListViewItem() As ListViewItem)
Call MyBase.AddRange(iListViewItem)
End Sub

Public Shadows Sub addRange(ByVal iListViewItemCollection As
listViewItem_Collection)
Call MyBase.AddRange(iListViewItemCollection)
End Sub

Public Shadows Function contains(ByVal iListViewItem As
ListViewItem) As Boolean
Return (MyBase.Contains(iListViewItem))
End Function

Public Shadows Function indexOf(ByVal iListViewItem As ListViewItem)
As Integer
Return (MyBase.IndexOf(iListViewItem))
End Function

Public Shadows Function insert(ByVal iIndex As Integer, ByVal
iListViewItem As ListViewItem)
Call MyBase.Insert(iIndex, iListViewItem)
End Function

Public Shadows Function insertRange(ByVal iIndex As Integer, ByVal
iListViewItem() As ListViewItem)
Call MyBase.InsertRange(iIndex, iListViewItem)
End Function

Public Shadows Function insertRange(ByVal iIndex As Integer, ByVal
iListViewItemCollection As listViewItem_Collection)
Call MyBase.InsertRange(iIndex, iListViewItemCollection)
End Function

Public Shadows Function lastIndexOf(ByVal iListViewItem As
ListViewItem) As Integer
Return (MyBase.LastIndexOf(iListViewItem))
End Function

Public Shadows Sub remove(ByVal iListViewItem As ListViewItem)
Call MyBase.Remove(iListViewItem)
End Sub

Public Shadows Function repeat(ByVal iListViewItem As ListViewItem,
ByVal iCount As Integer) As listViewItem_Collection
Return (MyBase.Repeat(iListViewItem, iCount))
End Function

Public Shadows Sub setRange(ByVal iIndex As Integer, ByVal
iListViewItemCollection As listViewItem_Collection)
Call MyBase.SetRange(iIndex, iListViewItemCollection)
End Sub

Public Shadows Sub setRange(ByVal iIndex As Integer, ByVal
iListViewItem() As ListViewItem)
Call MyBase.SetRange(iIndex, iListViewItem)
End Sub

Public Shadows Function toArray() As ListViewItem()
Return (MyBase.ToArray())
End Function

#End Region

End Class

----------------------------------------------------

Take note that I am only shadowing methods or properties that refer to type
"Object".

Nick.

"Jay B. Harlow [MVP - Outlook]" <Jay_H...@email.msn.com> wrote in message
news:OZl8PGYa...@TK2MSFTNGP10.phx.gbl...

Jay B. Harlow [MVP - Outlook]

unread,
Aug 23, 2003, 11:01:57 AM8/23/03
to
Nak,
Yes! I am saying you should not be using Shadowing! There are a handful of
times when you need it, this is not one of them!

Derive from CollectionBase, then every place in your code you are using
ByBase, use InnerList. Drop all the Shadows keywords.

If there is a chance you will use this collection via IList, override
OnValidate

Public Class listViewItem_Collection
Inherits CollectionBase

Public Function Add(ByVal iListViewItem As ListViewItem) _
As Integer
Return InnerList.Add(iListViewItem)
End Function

Public Sub AddRange(ByVal iListViewItem() As ListViewItem)
InnerList.AddRange(iListViewItem)
End Sub

' use for extra protection, if this collection will be accessed
' via the IList interface.
Protected Overrides Sub OnValidate(ByVal value As Object)
If Not TypeOf value Is ListViewItem Then
Throw New InvalidCastException
End If
End If

End Class

Also do not define Finalize unless you really need to Finalize (not just
calling the base Finalize). Your object will linger in memory much longer
then it needs to.

Hope this helps
Jay

"Nak" <a...@a.com> wrote in message

news:OVO6WQYa...@tk2msftngp13.phx.gbl...

Nak

unread,
Aug 23, 2003, 11:55:47 AM8/23/03
to
Hi Jay,

Thanks for the advice, I had no idea there was an inner list, I saw a
small example of inheriting from ArrayList but the example didn't mention
InnerList (not that I am aware of anyway, and not that I am renowned for my
reading skills). Thanks again, I shall modify my code to always inherit
from CollectionBase :-)

Nick.

P.S. The empty finalize code was only there as I had previously removed
license disposing code and forgot to remove the rest :-)

"Jay B. Harlow [MVP - Outlook]" <Jay_H...@email.msn.com> wrote in message

news:%238XJEeY...@TK2MSFTNGP09.phx.gbl...

Nak

unread,
Aug 23, 2003, 12:05:03 PM8/23/03
to
> Thanks for the advice, I had no idea there was an inner list, I saw a
> small example of inheriting from ArrayList but the example didn't mention
> InnerList (not that I am aware of anyway, and not that I am renowned for
my
> reading skills).

But then again, that's no excuse, I should have checked, sorry.

Nick.


Nak

unread,
Aug 23, 2003, 12:10:58 PM8/23/03
to
Looks like I've made another F**k up, I have done something very similar but
inherited from "SortedList", I need the sorting properties of this, is there
a way to have a sorted list via CollectionBase? My head so isn't working
today, I think I might give things a break!!

:-(

Nick.

"Nak" <a...@a.com> wrote in message

news:O8wJRBZ...@TK2MSFTNGP10.phx.gbl...

Jay B. Harlow [MVP - Outlook]

unread,
Aug 23, 2003, 12:14:38 PM8/23/03
to
Nak,
Actually CollectionBase has both InnerList & List protected properties.

CollectionBase.InnerList: represents the contained ArrayList itself, the On*
overridable methods will not be called (read InnerList has no validation).

CollectionBase.List: represents the 'logical' IList interface that
CollectionBase implements, the On* overridable methods will be called (read
List has validation).

Most of the time you want to use CollectionBase.InnerList property,
sometimes you need to use the CollectionBase.List property. For example, use
List.Add if you want to attach event handlers no matter how the item was
added, then override OnAdd to add the handlers...

Hope this helps
Jay

"Nak" <a...@a.com> wrote in message

news:%23M7oG8Y...@tk2msftngp13.phx.gbl...

Jay B. Harlow [MVP - Outlook]

unread,
Aug 23, 2003, 1:00:02 PM8/23/03
to
Nak,
For SortedList I would consider using a DictionaryBase, as DictionaryBase
starts with name/value pairs. Unfortunately DictionaryBase by itself does
not support being sorted.

If sorting was a "true" requirement, I would probably write my own
SortedDictionaryBase that contains a SortedList object. That has an
interface similiar to DictionaryBase. Most of the methods on
SortedDictionaryBase would delegate to the SortedList.

Hope this helps
Jay

"Nak" <a...@a.com> wrote in message

news:%23BQQlEZ...@TK2MSFTNGP12.phx.gbl...

Nak

unread,
Aug 23, 2003, 4:17:54 PM8/23/03
to
Jay,

Thanks so much, :-) I shall try it the second I get a spare moment,
thanks for your efforts they are much appreciated. I can't wait to try it
out, thanks again :-)

Nick.

"Jay B. Harlow [MVP - Outlook]" <Jay_H...@email.msn.com> wrote in message

news:OGCvYKba...@TK2MSFTNGP09.phx.gbl...
> Nak,
> Attached are extended CollectionBase & DictonaryBase classes, that should
do
> what you want.
>
> I wrote them to mimic CollectionBase & DictionaryBase as close as
possible.
> The differences being is they have a constructor that you can pass the
inner
> container to the constructor. And the InnerList changed to an IList type,
> while InnerHashtable changed to InnerDictionary of type IDictionary.
>
> ExCollectionBase accepts an IList in the constructor, if you do not give
> one, ArrayList is used. For example you could pass an array of objects,
> making a fixed size list, instead of an ArrayList. InnerList does not do
> validation, List does validation.
>
> ExDictionaryBase accepts an IDictionary in the constructor, if you do not
> give one, HashTable is used. For example you can pass SortedList,
> HybridDictionary, ListDictionary, instead of a HashTable. InnerDictionary
> does not do validation, Dictionary does validation.
>
> You can derive from ExDictionaryBase then pass a new SortedList to the
> constructor.
>
> Something like:
> Public Class NakSortedList
> Inherits ExDictionaryBase
>
> Public Sub New()
> MyBase.New(New SortedList)
> End Sub
>
> ' other methods you want
>
> End Class
>
> FWIW: I actually started with the m_dictionary field in ExDictionaryBase
as
> type SortedList, but then noticed I did not rely on it specifically being
a
> SortedList, so I modified ExDictionaryBase to support any IDictionary,
> which IMHO actually makes the class far more flexible.
>
> If you use them, let me know if you find any problems with them.
>
> Hope this helps
> Jay


>
> "Nak" <a...@a.com> wrote in message

> news:%23BQQlEZ...@TK2MSFTNGP12.phx.gbl...

Tom Spink

unread,
Aug 23, 2003, 5:49:38 PM8/23/03
to
AddRange is actually easily implemented:

Public Sub AddRange(items() As Object)

--
Happy to help,
-- Tom Spink
(thomas...@ntlworld.com)

"Go down with your server"

http://dotnetx.betasafe.com >> On The Mend

Please respond to the newsgroup,
so all can benefit


"Nak" <a...@a.com> wrote in message

news:OVO6WQYa...@tk2msftngp13.phx.gbl...

Nak

unread,
Aug 23, 2003, 6:14:02 PM8/23/03
to
Tom,

I think you missed the point, I don't want Object as the type, I wanted
Rectangle, but don't worry about it, they put me straight on that one,
Rectangle is a structure and not an object, hence it not type casting.

Nick.

"Tom Spink" <thomas...@ntlworld.com> wrote in message
news:e1u6mCca...@TK2MSFTNGP09.phx.gbl...

Tom Spink

unread,
Aug 24, 2003, 3:04:18 PM8/24/03
to
Ha, sorry, I clicked send before I'd finished my message, my mistake.
Actually, I pressed Ctrl+Enter, because I was in that sort of mood ;-))) <g>

--
Happy to help,
-- Tom Spink
(thomas...@ntlworld.com)

"Go down with your server"

Please respond to the newsgroup,
so all can benefit


"Nak" <a...@a.com> wrote in message

news:#dxRaPca...@TK2MSFTNGP09.phx.gbl...

Herfried K. Wagner [MVP]

unread,
Aug 24, 2003, 4:12:26 PM8/24/03
to
Hello,

"Tom Spink" <thomas...@ntlworld.com> schrieb:


> Ha, sorry, I clicked send before I'd finished my message, my mistake.
> Actually, I pressed Ctrl+Enter, because I was in that sort of mood
> ;-))) <g>

In my German OE Ctrl+Enter will store the message in the Drafts folder,
Alt+Enter is used to send a message.

Regards,
Herfried K. Wagner
--
MVP · VB Classic, VB .NET
http://www.mvps.org/dotnet


Tom Spink

unread,
Aug 24, 2003, 5:42:13 PM8/24/03
to
I'd just been using an irritating program, where to add another line in a
multiline textbox, you have to press Ctrl+Enter. Enter will accept the
entry, but I wanted multiline. I was still in Ctrl+Enter mode.

--
Happy to help,
-- Tom Spink
(thomas...@ntlworld.com)

"Go down with your server"

Please respond to the newsgroup,
so all can benefit


"Herfried K. Wagner [MVP]" <hirf...@m.activevb.de> wrote in message
news:OgVPKwna...@tk2msftngp13.phx.gbl...

Nak

unread,
Aug 24, 2003, 6:17:57 PM8/24/03
to
> I'd just been using an irritating program, where to add another line in a
> multiline textbox, you have to press Ctrl+Enter. Enter will accept the
> entry, but I wanted multiline. I was still in Ctrl+Enter mode.

Pressing 2 keys at once huh? I'll leave that to the pros! ;-)

Nick


Herfried K. Wagner [MVP]

unread,
Aug 24, 2003, 6:40:48 PM8/24/03
to
Hello Tom,

"Tom Spink" <thomas...@ntlworld.com> schrieb:


> I'd just been using an irritating program, where to add another line
in a
> multiline textbox, you have to press Ctrl+Enter. Enter will accept the
> entry, but I wanted multiline. I was still in Ctrl+Enter mode.


Which newsreader do you use?

Fergus Cooney

unread,
Aug 24, 2003, 9:05:22 PM8/24/03
to
LOL


Tom Spink

unread,
Aug 25, 2003, 4:07:08 AM8/25/03
to
OE, English, 6.00.2800.1106

The shortcut key Alt+Enter brings up properties, as it does for shell
objects, and I've just confirmed it by using Ctrl+Enter to send this
message, wierd no?

--
Happy to help,
-- Tom Spink
(thomas...@ntlworld.com)

"Go down with your server"

Please respond to the newsgroup,
so all can benefit


"Herfried K. Wagner [MVP]" <hirf...@m.activevb.de> wrote in message

news:eHsKEDpa...@tk2msftngp13.phx.gbl...

Tom Spink

unread,
Aug 25, 2003, 4:07:56 AM8/25/03
to
Certainly should leave it to the pros, the Professional Developers who can
actually design an application properley.

--
Happy to help,
-- Tom Spink
(thomas...@ntlworld.com)

"Go down with your server"

Please respond to the newsgroup,
so all can benefit

"Nak" <a...@a.com> wrote in message

news:e5fgQ2oa...@tk2msftngp13.phx.gbl...

Nak

unread,
Aug 25, 2003, 5:09:47 AM8/25/03
to
> Certainly should leave it to the pros, the Professional Developers who can
> actually design an application properley.

Haha, as opposed to?

Nick.


Herfried K. Wagner [MVP]

unread,
Aug 25, 2003, 9:18:55 AM8/25/03
to
Hello,

"Tom Spink" <thomas...@ntlworld.com> schrieb:
> OE, English, 6.00.2800.1106

I saw this in the message's properties.

> The shortcut key Alt+Enter brings up properties, as it does for shell
> objects, and I've just confirmed it by using Ctrl+Enter to send this
> message, wierd no?

Really wierd.

Tom Spink

unread,
Aug 25, 2003, 11:43:55 AM8/25/03
to
The...err...non-pros, who...err...can't make a simple textbox accept enter
for a new line. ;-)))

--
Happy to help,
-- Tom Spink
(thomas...@ntlworld.com)

"Go down with your server"

Please respond to the newsgroup,
so all can benefit


"Nak" <a...@a.com> wrote in message

news:eW8ediua...@TK2MSFTNGP09.phx.gbl...

Nak

unread,
Aug 25, 2003, 2:00:25 PM8/25/03
to
But that's devils work!!! noone can do that!!! ;-)

Nick.

"Tom Spink" <thomas...@ntlworld.com> wrote in message

news:%23io88$xaDHA...@tk2msftngp13.phx.gbl...

Tom Spink

unread,
Aug 26, 2003, 5:41:05 AM8/26/03
to
Naturlich.

--
Happy to help,
-- Tom Spink
(thomas...@ntlworld.com)

"Go down with your server"

Please respond to the newsgroup,
so all can benefit


"Nak" <a...@a.com> wrote in message

news:O$V$7KzaDH...@TK2MSFTNGP09.phx.gbl...

Nak

unread,
Aug 26, 2003, 6:57:10 AM8/26/03
to
> Naturlich.

What's a "Naturlich" when it's at home?

Nick.


Herfried K. Wagner [MVP]

unread,
Aug 26, 2003, 8:11:25 AM8/26/03
to
Hi Tom,

"Tom Spink" <thomas...@ntlworld.com> schrieb:
> Naturlich.

Natürlich.

"u" <> "ü"

Nak

unread,
Aug 26, 2003, 9:32:31 AM8/26/03
to
> Natürlich.

But what does it mean?

Nick.


Herfried K. Wagner [MVP]

unread,
Aug 26, 2003, 9:51:05 AM8/26/03
to
Hello,

"Nak" <a...@a.com> schrieb:


> > Natürlich.
>
> But what does it mean?

adj.: natural, normal,
adv.: naturally, of course

Tom Spink

unread,
Aug 26, 2003, 9:57:30 AM8/26/03
to
I haven't got a diaresis (umlaut) key, and I also haven't got enough RAM to
load CharMap ;-) LOL

--
Happy to help,
-- Tom Spink
(thomas...@ntlworld.com)

"Go down with your server"

Please respond to the newsgroup,
so all can benefit

"Herfried K. Wagner [MVP]" <hirf...@m.activevb.de> wrote in message

news:OI5k0t8...@tk2msftngp13.phx.gbl...

Tom Spink

unread,
Aug 26, 2003, 10:00:51 AM8/26/03
to
(I'm very good at German, I have German pen pals, also, my best friend
visited Austria the other week)

--
Happy to help,
-- Tom Spink
(thomas...@ntlworld.com)

"Go down with your server"

Please respond to the newsgroup,
so all can benefit

"Tom Spink" <thomas...@ntlworld.com> wrote in message

news:uExeDp9a...@tk2msftngp13.phx.gbl...

Herfried K. Wagner [MVP]

unread,
Aug 26, 2003, 11:10:41 AM8/26/03
to
Hello,

"Tom Spink" <thomas...@ntlworld.com> schrieb:


> I haven't got a diaresis (umlaut) key, and I also haven't got enough RAM
to
> load CharMap ;-) LOL

Maybe you should close VS .NET.

;-)))

Tom Spink

unread,
Aug 26, 2003, 2:55:42 PM8/26/03
to
ROFL. Was gonna mention that, but decided against it.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit


"Herfried K. Wagner [MVP]" <hirf...@m.activevb.de> wrote in message

news:O#b4LR#aDHA...@TK2MSFTNGP12.phx.gbl...

Herfried K. Wagner [MVP]

unread,
Aug 26, 2003, 3:32:22 PM8/26/03
to
Hello,

"Tom Spink" <thomas...@ntlworld.com> schrieb:
> Über Geek

ROFLM*O

Cor

unread,
Aug 26, 2003, 4:10:56 PM8/26/03
to
Herfried,
Do you see that nobody says something about "umlout" sensitive?
But I thought that can give the same problems (maybe even bigger with
different code sets) as case sensitive ?
This is a minor serious question, I am just curious.
Cor


Nak

unread,
Aug 26, 2003, 4:37:09 PM8/26/03
to
Wow I think you and Cor should start up a Herfried fan club!

Not that you don't deserve one of course Herfried I'm sure ;-)

Nick.

"Tom Spink" <thomas...@ntlworld.com> wrote in message

news:uieQ7q9a...@TK2MSFTNGP12.phx.gbl...

Herfried K. Wagner [MVP]

unread,
Aug 26, 2003, 4:56:14 PM8/26/03
to
Hello,

"Cor" <n...@non.com> schrieb:

A good idea. I hate the "Umlaute" and i hate the "ß" character in the
German language (I always write "ss" instead of "ß").

Some developers use umlauts when naming their variables *brrr*.

Tom Spink

unread,
Aug 26, 2003, 5:50:44 PM8/26/03
to
I think I might buy a German keyboard. I know that Z and Y are swapped, but
you get more buttons. Now Chinese, with about 5 shift keys, don't get me
started.

Umlaut sensitive sounds intriguing, and I have to agree with you, Herfried,
about the s-zet letter. I prefer ss.

Ich heisse Tom oder Ich heiße Tom?

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit


"Herfried K. Wagner [MVP]" <hirf...@m.activevb.de> wrote in message

news:eb#oHSBbD...@TK2MSFTNGP12.phx.gbl...

Tom Spink

unread,
Aug 26, 2003, 5:51:17 PM8/26/03
to
I'm going to make a T-Shirt with that on it.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit


"Herfried K. Wagner [MVP]" <hirf...@m.activevb.de> wrote in message

news:udDmcjAb...@tk2msftngp13.phx.gbl...

Nak

unread,
Aug 26, 2003, 7:18:33 PM8/26/03
to
What?

> -- Tom Spink, Über Geek

Nick.


Tom Spink

unread,
Aug 27, 2003, 5:10:13 AM8/27/03
to
Yup.

--
HTH,


-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit


"Nak" <a...@a.com> wrote in message

news:OK6tYhCb...@TK2MSFTNGP09.phx.gbl...

0 new messages