3

I need to have a player equip an item as shown here:

IEquiptable is an interface.

and this method is in my in player class.

public void equip(IEquiptable equipable) 
    {
        switch (equipable.GetType())
        {
            case equipable is Weapons:
                this.weapon = equipable;
                break;

            case equipable is Shield:
                this.shield = equipable
                break;

                //etc etc..
        }
    }

I get an error that the switch experession must be a bool,char,string,integral, enum or corresponding nullable type.

I could handle it by having an equit method in each of my weapon/shield etc classes and pass over my player class as a parameter. But I feel this is slightly illogical as a player should equipt an item, not an item equip it's self on the player.

1
  • What's the type of Player.weapon and Player.shield?
    – Jon
    Oct 6, 2013 at 17:47

3 Answers 3

3

You already have an interface so why don't you make use of it?

public interface IEquipable
{
    void EquipOn( Player player );
}

public class Shield : IEquipable
{
    public void EquipOn( Player player )
    {
        player.shield = this;
    }
}

public class Weapons : IEquipable
{
    public void EquipOn( Player player )
    {
        player.weapon = this;
    }
}

and your code becomes

public void equip(IEquiptable equipable) 
{
   equipable.EquipOn( this ); 
}

In fact, you don't even need this method, as it has been reduced to merely forwarding its job to proper class. Everytime you have your IEquipable you just call its EquipOn method.

4
  • It's what I originally had planned to do, just it felt slightly illogical to have the item equip it's self on the player.
    – William
    Oct 6, 2013 at 17:50
  • 1
    This is because instead of two interfaces, IShield and IWeapon you decided to have just one. Sonner or later you will have to violate the Interface Segregation Principle (if you put too much different responsibilities into IEquipable) or - if you don't - the interface won't be as usable as it should be. Try to separate these responsibilities and your original design where the Player class has overloaded methods to equip weapons and shields will again be valid. Oct 6, 2013 at 18:01
  • My Weapon and Shield both extend a base abstract class of Item There is unique functionality in both Weapon and Shield class, but I knew that both of these items can be equipted by the player in a slightly different way, which I thought perfect chance for an interface. Just I find it difficult to know where to place the logic from he interface.
    – William
    Oct 6, 2013 at 18:20
  • I notice a lot of examples for interfaces use vehicle examples: Car implements IDriveable But it's a person who drives the car, surely the method Drive should be in a Human/Player/Driver or whatever class
    – William
    Oct 6, 2013 at 18:21
1
public void equip(IEquiptable equipable) 
{
   var type = equipable.GetType();
   if(type is Weapons)
   {
    this.weapon = equipable;
    return;
   }
   if(type is Shield)
   {
     this.shield = equipable;
     return;
   }
   //etc
}
0

Because of the values of cases must be constants, the conventional solution would be to create an enum ..

public enum EquippableTypes {
    Weapons,
    Shield,
    Antenna,
    Underwear
}

public partial class Player {
    public void Equip(IEquippable equippable) {
        switch(equippable.Type) {
            case EquippableTypes.Weapons:
                this.weapon=equippable;
                break;

            case EquippableTypes.Shield:
                this.shield=equippable;
                break;

            case EquippableTypes.Antenna:
                this.antenna=equippable;
                break;

            case EquippableTypes.Underwear:
                this.underwear=equippable;
                break;

            // ... 
        }
    }

    IEquippable weapon, shield, antenna, underwear;
}

So your equippable interface/classes would look like that ..

public partial interface IEquippable {
    EquippableTypes Type {
        get;
    }
}

public partial class Weapons: IEquippable {
    public EquippableTypes Type {
        get {
            return EquippableTypes.Weapons;
        }
    }
}

public partial class Shield: IEquippable {
    public EquippableTypes Type {
        get {
            return EquippableTypes.Shield;
        }
    }
}

public partial class Antenna: IEquippable {
    public EquippableTypes Type {
        get {
            return EquippableTypes.Antenna;
        }
    }
}

public partial class Underwear: IEquippable {
    public EquippableTypes Type {
        get {
            return EquippableTypes.Underwear;
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.