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