[C#]Questions on class extension.

08/15/2014 01:48 Konvict-#1
I don't really know the terminology for what i'm trying to accomplish, but essentially what i'm trying to do is the following:
Code:
Character.Equipment.Equip(Item, Position);
But what im trying to avoid is having to create a class for equipment. Is there any way to contain the "Equipment" class methods and dictionaries without creating an entire new class for it? Id still like to be able to access it the same way, but without a new class.

#edit: Realized i put this in the wrong section. My apologies. Id move it if I could.
08/15/2014 01:51 Terreox#2
I guess you're looking for static classes and static methods. Just search for it on Google and you will get plenty of examples.
08/15/2014 01:53 Konvict-#3
Quote:
Originally Posted by Terreox View Post
I guess you're looking for static classes and static methods. Just search for it on Google and you will get plenty of examples.
I still need it to be a new instance for every new instance of a character is the only issue with that unfortunately. (Need a new dictionary etc per character class)

Basically just want some sort of internal namespace for the class haha. Just for organizational purposes.
08/15/2014 01:59 Terreox#4
Then I don't really understand what you're trying to achieve.

Then I don't really understand what you're trying to achieve.

Edit: if you have an existing class called Equipment and you just want to add a method to it without extending the Equipment class, then you have to look for extension methods.
08/15/2014 02:07 Konvict-#5
Quote:
Originally Posted by Terreox View Post
Then I don't really understand what you're trying to achieve.
Currently I have it setup such as the following:

Character.cs
Code:
public class Character
{
     public Equipment Equipment;
     public Character()
     {
          this.Equipment = new Equipment(this);
     }
}
Equipment.cs
Code:
public class Equipment
{
     Dictionary<uint, Item> Items = new Dictionary<uint, Item>();
     private Character Owner;
     public Equipment(Character Owner) { this.Owner = Owner; }
}
Im basically trying to combine the 2 classes, without changing the way I access the equipment methods. Im trying to keep the way its organized, without creating a new class.

I dont believe its possible, but just trying something new hah.
08/15/2014 02:18 Terreox#6
I think that's impossible.
If you have e.g. a method Foo() in your Equipment class and you would access it from your Character class like this:
Code:
Character.Equipment.Foo()
...and now you merged Equipment with Character so that you essentially have a Character class with a Foo() method, then you can't access it the same way.
If there is no Equipment class then there is no Equipment.Foo() method.
08/15/2014 08:42 Mostey#7
Quote:
Originally Posted by Konvict- View Post
I dont believe its possible, but just trying something new hah.
It isn't. Just imagine what'd happen if this would be possible. I actually don't see the point in doing this, why don't you just walk over the way to your equipment? Why do you want to merge these classes? If you want to access the methods this way, then just declare and define your Equipment class methods and fields in your character class. Simply as that.
08/19/2014 23:42 MrSm!th#8
What you're trying to do doesn't even make sense.
If there are few equipment methods and those do simple tasks only, you may consider putting them into your Character class.
If there is a complex logic behind the Equipment concept, it's totally acceptable (and favorable) to create a separate class for it.

You can still create a property in your Character class to access the Equipment instance:

Code:
//Equipment class definition omitted

public class Character
{
    public Equipment Equipment { get; set; }
    public Character() 
    {
        Equipment = new Equipment(this);
    }
}

Character c = new Character();
c.Equipment.EquipStuff();
Why do you want to merge them? The fact you are trying to achieve that and that your Equipment class needs a reference to your Character object tell me that Equipment wants to access some data of Character, is that right?
There could be your actual problem. I doubt that equipment needs to know who wears it, so if your's does, your class design is wrong.