How to access properties from an array of class objects in C #?

    Character[] PlayerOne = new Character[5];

    PlayerOne[1] = new BladeWarrior();
    PlayerOne[2] = new FistWarrior();
    PlayerOne[3] = new Archer();
    PlayerOne[4] = new RedMage();
    PlayerOne[5] = new BlueMage();  

I know through polymorphism, BladeWarrior can be a character, but it cannot be the other way around. My problem is when I try to access an element of an array. For example, Player [1], I cannot use the access functions and variables from the BladeWarrior class. This allows me access to variables and functions from the Character class, which inherits from the BladeWarrior class.

I will need to have access to Bladewarrior functions / variables if Im wants 2 characters to fight.

I thought I could use the how-to function to set PlayerOne [1] as a specific object. Not certainly in that way:

string s = objArray[i] as string;

The line of code above is just an idea that I am talking about.

What is the solution to this problem?

+4
6

/ Bladewarrior, Im , 2 .

, :

Fight(PlayerOne[i], PlayerOne[j]);

, . , , , . # 4, dynamic :

static class FightArena {
    public static void Fight(dynamic a, dynamic b) {
        try {
            DoFight(a, b);
        } catch {
            Console.Error.WriteLine("{0} and {1} cannot fight", a, b);
        }
    }
    private static void DoFight(BladeWarrior a, Archer b) {
    }
    private static void DoFight(BladeWarrior a, FistWarrior b) {
    }
    private static void DoFight(BladeWarrior a, RedMage b) {
    }
    private static void DoFight(BladeWarrior a, BlueMage b) {
    }
    private static void DoFight(BladeWarrior a, BladeWarrior b) {
    }
    private static void DoFight(Archer a, Archer b) {
    }
    ... // Enumerate all pairs that can fight
}

:

FightArena.Fight(PlayerOne[i], PlayerOne[j]);

PlayerOne[i] PlayerOne[j].

+2

Character , , , . Character Bladewarrior, , , , . , , , .

, , , Character, . Character, , .

, Attacks Character, , , . Attacks, . , , .

, Character, . .

+1

, BladeWarrior, Character BladeWarrior, as :

BladeWarrior bw = PlayerOne[1] as BladeWarrior;
0

, parent .

, .

BladeWarrior player1 = new BladeWarrior();
FistWarrior player2 = new FistWarrior();
Archer player3 = new Archer();
//and so on
0

, "" . , , , .

var currentCharacter = PlayerOne[1] as BladeWarrior;
if(currentCharacter  != null)
{
....
}

, , . , underliying

switch(PlayerOne[1].Type)
{
    case PlayerTypes.BladeWarrior:
    currentCharacter = PlayerOne[1].Character as BladeWarrior;
}

, ( L SOLID). , ( ) .

0

public interface ICharacter {
    int myValue { get; set;}
    void myMethod();
}

public class BladeWarrior : ICharacter {
    private int myPrivateValue;
    public int myValue { get { return myPrivateValue; } set { myPrivateValue = value; } }

    public void myMethod() {
    //Do what you want
    }
}


ICharacter[] PlayerOne = new ICharacter[5];
PlayerOne[0] = new BladeWarrior();

ICharacter [0].myMethod();

0

All Articles