0

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.

2
  • 2
    Why do you use structs rather than classes there? Commented Nov 6, 2013 at 9:08
  • I wanted to practice using structs. But I will change the player to a class actually. Commented Nov 6, 2013 at 10:23

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes they should be public because I want to use them in another class I just forgot to change that. Aside from that I will have to read more about structs vs classes. Thank you.
0

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!

2 Comments

You certainly can create a struct with the new keyword, see msdn.microsoft.com/en-us/library/0taef578.aspx
Frankly, I do not understand why this answer gets two upvotes. The code shown is not compileable in C# and the explanation is incorrect. @lauCosma, could you correct or delete the answer, please, so future visitors are not confused?

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.