|
||
|
|
#1 (permalink) |
|
Forum Expert
|
It's been a SUPER long time since I've worked with C#, so I'm a bit fuzzy on this one.
Here's what I've got: I have two classes; Player, and AIPlayer. AIPlayer is derived from Player. I want to use a container that will hold both types, and be able to call methods from each object as its own class. I've attempted ArrayList, using similar to the following code: Code:
System.Collections.ArrayList players = new System.Collections.ArrayList(); Player one = new Player(); AIPlayer two = new AIPlayer(); players.Add(one); players.Add(two); players[0].Move; players[1].Move; ...etc Ideas? |
|
|
|
|
|
#2 (permalink) |
|
Master of the Internet
Join Date: Oct 2005
Age: 44
Posts: 6,283
|
If you use an arraylist, the arraylist has no types, it can only return an object. You will have to test for type, and then cast it accordingly. Or, you can use a Generic List which you can type (if you are using .Net 2.0), and make it of the base type:
Code:
List<Player> list = new List<Player> ();
__________________
Why is it that I'm never as smart as I thought I was yesterday? My vast knowledge is only surpassed by my infinite ignorance. <TheOutkastDev> i might have to hire an assassin to killl mal so that i can jump in front of the bullet and piss on him |
|
|
|
|
|
#4 (permalink) |
|
ConnectUO Creator
Join Date: Jan 2004
Location: In your mom
Age: 27
Posts: 4,762
|
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 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();
__________________
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 |
|
|
|
|
|
#5 (permalink) |
|
Forum Expert
|
It helps a lot. I've got it sorta working. When a property (move) is called, regardless of if it's Player or AIPlayer, the Player's move is used. How would I specify that the AIPlayer's move needs to be used when the type is AIPlayer?
Let me know if you need to see my declarations for everything, I'll attach on the next post. Thanks |
|
|
|
|
|
#6 (permalink) | |
|
ConnectUO Creator
Join Date: Jan 2004
Location: In your mom
Age: 27
Posts: 4,762
|
Quote:
__________________
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 |
|
|
|
|
|
|
#8 (permalink) | |
|
ConnectUO Creator
Join Date: Jan 2004
Location: In your mom
Age: 27
Posts: 4,762
|
Quote:
Examples Code:
public class BaseClass
{
public BaseClass(){}
public virtual void Foo() {}
}
public class InheretedClass : BaseClass
{
public InheretedClass() : base() {}
public override void Foo() {}
}
__________________
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 |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|