简体   繁体   English

c#:将struct对象传递给struct对象

[英]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. 然后玩家将获得7个特殊技能和13个技能。 To make it more readable (in my oppinion) I use structs for specials, skills and players. 为了使它更具可读性(在我的意见中),我使用结构特殊,技能和玩家。 This is also for prectice using structs.. 这也适用于使用结构的prectice ..

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? 这会创建一个名为charisma的新特殊对象,对吧? 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. 其次,你不能在结构中使用实例字段初始值设定项,所以基本上你的整个player结构都是无效的,所以我不知道你是如何得到你描述的错误而没有遇到这个问题。

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. Struct是VALUE TYPES ,这意味着您无法使用new关键字对它们进行实例化。 Try directly with 直接尝试

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

Also checkout : http://msdn.microsoft.com/en-us/library/s1ax56ch.aspx Hope this helps! 还有结帐: http//msdn.microsoft.com/en-us/library/s1ax56ch.aspx希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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