Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > .NET Languages
You last visited: Today at 00:06

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[C#]Questions on class extension.

Discussion on [C#]Questions on class extension. within the .NET Languages forum part of the Coders Den category.

Reply
 
Old   #1
 
Konvict-'s Avatar
 
elite*gold: 0
Join Date: Aug 2014
Posts: 16
Received Thanks: 2
[C#]Questions on class extension.

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.
Konvict- is offline  
Old 08/15/2014, 01:51   #2

 
Terreox's Avatar
 
elite*gold: 155
Join Date: Aug 2009
Posts: 628
Received Thanks: 153
I guess you're looking for static classes and static methods. Just search for it on Google and you will get plenty of examples.
Terreox is offline  
Old 08/15/2014, 01:53   #3
 
Konvict-'s Avatar
 
elite*gold: 0
Join Date: Aug 2014
Posts: 16
Received Thanks: 2
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.
Konvict- is offline  
Old 08/15/2014, 01:59   #4

 
Terreox's Avatar
 
elite*gold: 155
Join Date: Aug 2009
Posts: 628
Received Thanks: 153
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.
Terreox is offline  
Old 08/15/2014, 02:07   #5
 
Konvict-'s Avatar
 
elite*gold: 0
Join Date: Aug 2014
Posts: 16
Received Thanks: 2
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.
Konvict- is offline  
Old 08/15/2014, 02:18   #6

 
Terreox's Avatar
 
elite*gold: 155
Join Date: Aug 2009
Posts: 628
Received Thanks: 153
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.
Terreox is offline  
Old 08/15/2014, 08:42   #7


 
elite*gold: 1091
Join Date: Jun 2007
Posts: 19,836
Received Thanks: 7,180
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.
Mostey is offline  
Old 08/19/2014, 23:42   #8


 
MrSm!th's Avatar
 
elite*gold: 7110
Join Date: Jun 2009
Posts: 28,904
Received Thanks: 25,394
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.
MrSm!th is offline  
Reply


Similar Threads Similar Threads
[TuT] How To Change Pk2 Extension To Any Extension And Change File Name
06/16/2014 - SRO PServer Guides & Releases - 7 Replies
In This tutorial i'll show you how to change .pk2 extension and change the file name for anything you want . We can do it in two ways .. First using Ollydbg Second using Hex Workshop ....



All times are GMT +2. The time now is 00:06.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2024 elitepvpers All Rights Reserved.