1

First time posting a question because up untill now I've always found the answer I'm looking for here so bear with me. :)

On to the problem, I'm testing an interface I'm making in a console application to use in a game. I got an interface for a character, also made a class for it. In my main code I can succesfully call my "sCharacterName" from my class(Character1Class).

In my (SwordClass) I have a void for equiping the weapon and i want it to show like this: "(sCharacterName) equiped (sWeaponName)." But whenever I try it shows up as a blank space instead of the name. This is part of my code:

namespace ConsoleInterfaceTest
{
    class Program
    {
        public interface ICharacters
        {
            // The name of the character
            string sCharacterName { get; set; }
        }

        public interface IWeapon
        {
            // The name of the weapon
            string sWeaponName { get; set; }

            void Equip();
        }

        public class Character1Class : ICharacters
        {
            // The name of the character
            public string sCharacterName { get; set; }

            public Character1Class(string v)
            {
                sCharacterName = ("ZeeAars");
            }
        }

        public class SwordClass : IWeapon, Characters.ICharacters
        {
            // The name of the weapon
            public string sWeaponName { get; set; }

            public string sCharacterName { get; set; }

            public SwordClass(string sName)
            {
                sWeaponName = sName;
            }

            public void Equip()
            {
                Console.WriteLine(sCharacterName + " equiped " + sWeaponName + ".");
            }
        }

        public static void Main (string[] args)
        {
            Characters.Character1Class character1 = new Characters.Character1Class("");
            character1.CharacterDamage(50);


            Weapons.SwordClass sword = new Weapons.SwordClass("TestSword");
            sword.Equip ();

            Console.ReadKey();
        }
    }
}

Thanks in advance, really been scratching my head for a couple days with this problem :D

7
  • 2
    I can't really grasp what you're trying to do but I have a feeling that you have completely the wrong idea of what interfaces are for and what they do - nothing ever sets the sCharacterName property of your sword class - and why would a Sword be an ICharacter? Finding an introductory tutorial on interfaces might be a good idea (although I'm not sure they're the solution to your problem). Commented Sep 29, 2016 at 16:18
  • the way you have it set up, a weapon owns a character. That is going to cause you a lot of problems later. Commented Sep 29, 2016 at 16:20
  • It might be useful to know also that I would expect character.Equip(sword), not sword.Equip() (which makes no association with a character). Commented Sep 29, 2016 at 16:20
  • CharacterDamage() is not defined, your example wont compile. Agree with others, why would a sword implement ICharacter? Commented Sep 29, 2016 at 16:22
  • @DGibbs my suspicion is that he thinks that having two classes implementing the same interface makes them share properties (hence the suggestion to go and learn how interfaces work). Commented Sep 29, 2016 at 16:24

3 Answers 3

1

I agree with the other's comments that you need to rethink how you are using Interfaces and how you are structuring your object hierarchy, but to simply answer the asked question;

Weapons.SwordClass sword = new Weapons.SwordClass("TestSword");
// Add the following line to your code
sword.sCharacterName = character1.sCharacterName;
sword.Equip();
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, it works. I have read the comments and realise my stupidity. Learned myself interfaces a week ago, so I guess I need to do alot more research for it. If you have any posts, articles or tutorials you used to understand them please tell me as I can really use the help :D
0

I think you are looking to do something like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
class Program
{
    public static void Main(string[] args)
    {
        Character1Class character = new Character1Class("Zoltan the Powerful");
        SwordClass sword = new SwordClass("Holy Avenger");
        character.Equip(sword);
        character.Attack();


        Console.ReadKey();
    }

    public interface ICharacters
    {
        // The name of the character
        string sCharacterName { get; set; }
        void Equip(IWeapon weapon);
    }

    public interface IWeapon
    {
        // The name of the weapon
        string sWeaponName { get; set; }

        int Attack();
    }

    public class Character1Class : ICharacters
    {
        // The name of the character
        public string sCharacterName { get; set; }

        public Character1Class(string charName)
        {
            sCharacterName = charName;
        }

        public void Equip(IWeapon weapon)
        {
            MyWeapon = weapon;
            Console.WriteLine(sCharacterName + " equiped " + weapon.sWeaponName + ".");
        }

        public IWeapon MyWeapon { get; set; }

        public void Attack()
        {
            if (MyWeapon == null)
            {
                Console.WriteLine(sCharacterName + " punches for 1 pt of damage.");
            }
            else
            {
                Console.WriteLine(string.Format("{0} swings for {1} pt of damage.", sCharacterName, MyWeapon.Attack()));
            }
        }
    }

    public class SwordClass : IWeapon
    {
        // The name of the weapon
        public string sWeaponName { get; set; }

        public SwordClass(string sName)
        {
            sWeaponName = sName;
        }


        public int Attack()
        {
            return 50;
        }

    }
}

}

Comments

0

Definitely think about the structure a bit more. Ask questions. Some possible questions include.

Does a weapon have a character or does a character own a weapon?
A character owns weapon(s).

So now we know weapons should be a property on character.

How many weapons can a character own? A character can own many weapons, but can only equip as many as their hands allow.

So now we know our weapons property will need to store more than one weapon but can equip a smaller number of them.

public interface ICharacters
{
    string sCharacterName { get; set; }
    List<IWeapon> Weapons { get; }
    void Equip(IWeapon toEquip);
}

public interface IWeapon
{
    string sWeaponName { get; }
}

public class Character1Class : ICharacters
{
    public string sCharacterName { get; set; }
    public List<IWeapon> Weapons { get; }

    public Character1Class(string v)
    {
        Weapons = new List<IWeapon>();
        sCharacterName = v;
    }

    public void Equip(IWeapon toEquip)
    {
        if (Weapons.Contains(toEquip))
            Console.WriteLine(sCharacterName + " equiped " + toEquip + ".");
        else Console.WriteLine(sCharacterName + " doesn't have " + toEquip + " to equip.");
    }
}

public class SwordClass : IWeapon
{
    public string sWeaponName { get; }

    public SwordClass(string sName)
    {
        sWeaponName = sName;
    }
}

class Program
{
    public static void Main(string[] args)
    {
        Character1Class character1 = new Character1Class("Frodo");
        SwordClass sword = new SwordClass("TestSword");
        character1.Weapons.Add(sword);
        character1.Equip(sword);
        Console.ReadKey();
    }
}

5 Comments

How can I improve my code structure more, I would love to learn that to be able to formulate my code better in the future.
There are a lot of books on design patterns, object oriented designs out there. If you enjoy a more do it yourself approach, start by asking questions like the ones in this post. I'll expand the post with more questions and answers.
Well I get lessons in c# on school but I talked to some friends in higher classes and they told me they wouldn't have learned this if not for a teacher that didn't know what else to teach them. So to rise to the top I will need to teach it myself hahaha. I got ''head first C#'', do you have any other recommendations for books or online tutorials?
My suggestion is to figure what kind of software you want to write and search StackOverflow for recommended reading. Some search topics include SOLID and ACID principles, Design Patterns, Unit Testing, Test Driven Development, and Inversion of Control. But that's only a start.
I will look into that, thank you for the advice and have a nice day :D

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.