View Single Post
Old 02-14-2007, 03:45 PM   #4 (permalink)
Jeff
ConnectUO Creator
 
Jeff's Avatar
 
Join Date: Jan 2004
Age: 28
Posts: 4,886
Default

Arraylists are object lists, whatever is in it, is assumed to be an object, thus when you get something out of it, you need to cast it as its type

Code:
System.Collections.ArrayList players = new System.Collections.ArrayList();
Player one = new Player();
AIPlayer two = new AIPlayer();
players.Add(one);
players.Add(two);

((Player)players[0]).Move;
((Player)players[1]).Move;
...etc
If you end up casting it as the wrong type, you will get a Invalid cast type exception and a crash.

Using a generic list, can give you a list that doesnt need casting. For example


Code:
System.Collections.Generics.List<Player> players = new System.Collections.Generics.List<Player>();
Player one = new Player();
AIPlayer two = new AIPlayer();
players.Add(one);
players.Add(two);

players[0].Move;//this will work
players[1].Move;//this will work

//If you needed to see if the specific player was a AIPlayer you could do this
if( players[0] is AIPlayer )
    ((AIPlayer)players[0]).DoThis();
Hope this helps
__________________
Jeff Boulanger
ConnectUO - Core Developer

Want to help make ConnectUO better? Click here to submit your ideas/requests
Use your talent to compete against other community members in RunUO hosted coding competitions

If you know XNA (even if its just a little) or are a good artist(2d or 3d) and are interested in making games for a hobby send me a pm or drop by #xna in irc.runuo.com. I'm looking to put together a small game development team.


Please do not pm me for support. If you are having issues please post in the appropriate forum. Thanks for your continued support of both ConnectUO and RunUO
Jeff is offline   Reply With Quote