arraylist - how do I retrieve a single element?

P

Peter Webb

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)?
 
J

Jon Skeet [C# MVP]

This is driving me crazy.

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
 
P

Peter Webb

Jon Skeet said:
This is driving me crazy.

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

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.
 
J

Jon Skeet [C# MVP]

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.

I have

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

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
 
P

Peter Webb

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).
 
J

Jon Skeet [C# MVP]

Peter Webb said:
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.
 

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