简体   繁体   中英

c#: passing struct object to struct object

I trying to create a "player" that has a set of skills and a set of specials. Each skill is associated with a certain special.

Then the player is given 7 specials and 13 skills. To make it more readable (in my oppinion) I use structs for specials, skills and players. This is also for prectice using structs..

Please have a look at my code, it is pretty straight forward.

private struct specials
{
   public string name;
   public int value;

   public specials(string n, int v)
   {
       this.name = n;
       this.value = v;
   }
}

private struct skills
{
    public string name;
    public specials spec;
    public int value;

    public skills(string n, specials s, int v)
    {
        this.name = n;
        this.spec = s;
        this.value = v;
    }
}

public struct player
{
    public specials strength = new specials("STRENGTH", 0);
    public specials perception = new specials("PERCEPTION", 0);
    public specials endurance = new specials("ENDURANCE", 0);
    public specials charisma = new specials("CHARISMA", 0);
    public specials intelligence = new specials("INTELLIGENCE", 0);
    public specials agility = new specials("AGILITIY", 0);
    public specials luck = new specials("LUCK", 0);

    //Complains about charisma, saying an object reference is required for 
    // the nonstatic field method or property
    public skills barter = new skills("Barter", charisma, 0); 
}

My problem you can see in the comment in the code. Now, I can't see why this should be a problem.

The player is given his own specials and own skills and the skill is in turn associated (I hope by reference) with a special.

public specials charisma = new specials("CHARISMA", 0);

This creates a new special-object named charisma, right? So why shouldn't this be able to pass to a new skill-object.

One last thing. For some reason I still don't understand but realy would like to know, this is solved by using static when declaring charisma but the I can't change it in my form-class by typing

player.charisma.value = 123;

Regards!


EDIT:

I just discovered something that I can't explain. Instead of writing (that produced an error)

public skills barter = new skills("Barter", charisma, 0);

I write: (player is now a class)

public skills barter = new skills();

Then in the players constructor I can set the barters special like this:

barter.spec = strength;

Why is this exepted? Is this bad practice? Well I wont use it anyway because if I change the players special it wont be updated in the barter because I learned that struct is passed by value not reference. But my question about why I cant initialize the structure with the struct still stands.

First of all, the first structs you define are private, which is weird (unless maybe they are nested in some other type).

Secondly, you can't have instance field initializers in structs, so basically your whole player struct is invalid so I don't know how you got the error you described without running into this first.

You should probably be using classes, anyway.

As for your final question, static members are associated with types , not with instances of said type, which is why you can't change them for individual instance.

Struct are VALUE TYPES which means that you cannot instantiate them with the new keyword. Try directly with

public specials charisma = specials("CHARISMA", 0)

Also checkout : http://msdn.microsoft.com/en-us/library/s1ax56ch.aspx Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM