Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / November 2007

Tip: Looking for answers? Try searching our database.

arraylist - how do I retrieve a single element?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Peter Webb - 05 Nov 2007 11:54 GMT
This is driving me crazy.

I have defined an arraylist as follows:

 public ArrayList drawnballs;

This is instantiated through

  drawnballs = new ArrayList();

I add balls to this just fine with:

drawnballs.Add(myball);

I can access every element (every ball) through a foreach statement, that
works great.

What I can't do is get just the first ball (or any individual ball).

anotherball = drawnballs[0] doesn't work. There is no method like
anotherball = drawnballs.Item[1]; which is what I would expect. I can't get
the drawnballs.getarray method to work.

Please, please, how do I reference a single ball in drawnballs (ie in an
arraylist)?
Jon Skeet [C# MVP] - 05 Nov 2007 12:07 GMT
On Nov 5, 11:54 am, "Peter Webb"
<webbfam...@DIESPAMDIEoptusnet.com.au> wrote:
> This is driving me crazy.

<snip>

> What I can't do is get just the first ball (or any individual ball).
>
> anotherball = drawnballs[0] doesn't work.

It does, but being a weakly typed ArrayList you need to cast it:

anotherball = (MyBall) drawnballs[0];

Are you still using C# 1? If you're using C# 2 (and .NET 2.0) then
you'd be better off using List<T>.

Jon
Peter Webb - 05 Nov 2007 14:28 GMT
> On Nov 5, 11:54 am, "Peter Webb"
> <webbfam...@DIESPAMDIEoptusnet.com.au> wrote:
[quoted text clipped - 14 lines]
>
> Jon

Thankyou - worked great. Pity the documentation didn't give an example (that
I could find) and I had even already searched for "cast" (because it
appeared in the error message) with no help.

I'm not ready for List<T> - I have only just got used to {}, {}, and []
without adding <>.

One very last question, sorry, also driving me crazy.

I have

Ball ball1 = new Ball();
Ball ball2 = new Ball():

it seems these are two separate objects, I can change their properties
independently.

But as soon as I go

ball2 = ball1;

It seems the system is pointing ball2 at ball1, and changes to a property of
one change the property of the other.

I just want to make a temporary working copy of ball1, but I don't want to
have to copy every property by hand.

How can I make a statement like

ball2 = ball1;

Simply copy all of the properties of ball1 to ball2 without making them the
same object?

Or am I totally misunderstanding something?

Thanks (hopefully) in advance.
Jon Skeet [C# MVP] - 05 Nov 2007 16:02 GMT
On Nov 5, 2:28 pm, "Peter Webb" <webbfam...@DIESPAMDIEoptusnet.com.au>
wrote:
> Thankyou - worked great. Pity the documentation didn't give an example (that
> I could find) and I had even already searched for "cast" (because it
> appeared in the error message) with no help.
>
> I'm not ready for List<T> - I have only just got used to {}, {}, and []
> without adding <>.

Okay - just bear in mind that when you *do* learn generics, you'll
want to start using them instead of ArrayLists :)

> One very last question, sorry, also driving me crazy.
>
[quoted text clipped - 5 lines]
> it seems these are two separate objects, I can change their properties
> independently.

Correct.

> But as soon as I go
>
> ball2 = ball1;
>
> It seems the system is pointing ball2 at ball1, and changes to a property of
> one change the property of the other.

It's more accurate to think of ball1 and ball2 as independent
variables which happen to refer to the same object. It's not that
there's an object called "ball1" and now "ball2" points at it - it's
that there's one object which both variables point at. Changing a
property with ball1.Foo = someValue; changes the property on the
object; the change is therefore visible via either ball1 or ball2.

> I just want to make a temporary working copy of ball1, but I don't want to
> have to copy every property by hand.

Sounds like you want to implement ICloneable; look into
object.MemberwiseClone to implement it, but bear in mind that some of
those properties may themselves be reference types.

> How can I make a statement like
>
> ball2 = ball1;
>
> Simply copy all of the properties of ball1 to ball2 without making them the
> same object?

You can't (while it's a class, anyway). You'd have to do

ball2 = ball1.Clone();

See http://pobox.com/~skeet/csharp/parameters.html
and
http://pobox.com/~skeet/csharp/memory.html
and
http://pobox.com/~skeet/csharp/references.html
for more information about value types and reference types.

Jon
Peter Webb - 06 Nov 2007 06:28 GMT
Exactly what I needed to know. The web pages you provided were great; I
browsed some of the other pages and picked up on a lot of other stuff that
helps explains what going on.

Its a long learning curve from 6502 assembler to C#, and I have enjoyed
almost every minute of it so far.

Thanks for your help, and good luck with your book (if you decide to
publish).
Jon Skeet [C# MVP] - 06 Nov 2007 07:47 GMT
> Exactly what I needed to know. The web pages you provided were great; I
> browsed some of the other pages and picked up on a lot of other stuff that
> helps explains what going on.

Excellent. Do ask again if any of it becomes difficult again. I've
found that I sometimes think I understand something, and then when I
try to use it something else goes wrong!

> Its a long learning curve from 6502 assembler to C#, and I have enjoyed
> almost every minute of it so far.

It's certainly a lot more productive than assembler :)

> Thanks for your help, and good luck with your book (if you decide to
> publish).

Oh yes, it's due out in March.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.