简体   繁体   English

c#在类的方法中访问结构成员

[英]c# accessing struct members within class' methods

i'm trying to write a simple game. 我正在尝试编写一个简单的游戏。 I want to design a "movement controller" object, that would handle move instructions from the engine. 我想设计一个“运动控制器”对象,该对象将处理来自引擎的运动指令。 I would also like for this object to expose a method, that would check its state and return true/false depending if it's ready to move or not. 我还希望该对象公开一个方法,该方法将检查其状态并根据是否准备移动来返回true / false。 The state will be saved as group of boolean variables. 状态将另存为一组布尔变量。 Since there is a lot of them, I decided to group them together in a new struct called "flags". 由于它们很多,因此我决定将它们归为一个名为“ flags”的新结构。 This is how it looks like more or less: 看起来或多或少是这样的:

public class movContr
{
    int movId;
    public struct flags
    {
        bool isMoving;
        bool isLocked;
        bool isReady;
        (...)
    }

    public bool CheckReadiness()
    {
        if(flags.isReady==true) return true;
        return false;
    }
}

Now the problem is this won't compile, error being: 现在的问题是,它将无法编译,错误是:

error CS0120: An object reference is required to access non-static member

offending line being: 违规行是:

if(flags.isReady==true) return true;

So I guess C# doesn't treat structs like memory blobs that just contains variables in orders, but like some "special" cousin of a class. 因此,我想C#不会像存储blob那样仅按顺序包含变量的结构,而是像某个类的“特殊”表亲那样对待结构。

Here are my questions: 这是我的问题:

how should I handle accessing struct class members in its methods? 我应该如何处理在其方法中访问结构类成员的方法? How can I refer in class methods to members of its future instances? 如何在类方法中引用其未来实例的成员? I tried this instead: 我尝试这样做:

if(this.flags.isReady==true) return true;

but i get the same error. 但我得到同样的错误。

Alternatively, if encapsulating my flag variables using 'struct' is not a proper way to do so, what is? 或者,如果使用'struct'封装我的标志变量不是正确的方法,那是什么?

I tried to find an answer myself but since all the keywords I could come up with are very generic, so are the results. 我试图自己找到一个答案,但由于我能拿出的所有关键字都非常通用,因此结果也很通用。 Most deal with static members, which are not a solution here since I need multiple, independent instances of movContr class. 大多数处理静态成员,在这里不是解决方案,因为我需要movContr类的多个独立实例。

You have created a declaration of struct called flags . 您已经创建了一个称为flags的结构声明。 But it is only declartion, not a concrete value. 但这仅仅是声明,而不是具体的价值。 So, the snippet 因此,摘要

if(flags.isReady==true) return true;

tries to access static member ( https://msdn.microsoft.com/en-us/library/98f28cdx.aspx ). 尝试访问静态成员( https://msdn.microsoft.com/zh-cn/library/98f28cdx.aspx )。

You need to declare a variable of that type in order to use it: 您需要声明该类型的变量才能使用它:

private flags myFlag;

public bool CheckReadiness()
{
     if(myFlag.isReady==true) return true;
      return false;
}

Maybe your confusion comes from C++, where "inline, anonymous" struct is allowed: 也许您的困惑来自C ++,其中允许使用“内联,匿名”结构:

struct {
    int some_var
} flags;

flags.some_var = 1;

It is not available in C# 它在C#中不可用

As exception suggests You need to create instance of struck as shown below :- 作为例外提示,您需要创建被触击的实例,如下所示:-

flags flag;
flag.isReady = true;

For more information :- 欲获得更多信息 :-

http://www.dotnetperls.com/struct http://www.dotnetperls.com/struct

Please notice how, in Main, the struct is created on the stack. 请注意在Main中如何在堆栈上创建结构。 No "new" keyword is used. 没有使用“新”关键字。 It is used like a value type such as int. 像值类型(如int)一样使用它。

By the way i would suggest you to use auto implemented properties instead of Struck if this is the only use as shown in your code 顺便说一句,我建议您使用自动实现的属性而不是Struck,如果这是代码中所示的唯一用途

Ref:- https://msdn.microsoft.com/en-us/library/bb384054.aspx 参考:-https: //msdn.microsoft.com/en-us/library/bb384054.aspx

You can't use flags , because you are only declaring a datatype with "struct flags". 您不能使用flags ,因为您仅使用“结构标志”声明数据类型。 You need to create an instance. 您需要创建一个实例。

and you should declare the fields of the struct as public. 并且您应该将结构的字段声明为public。 otherwise you can't access them. 否则您将无法访问它们。

    int movId;

    // Delace fields as public
    public struct flags
    {
        public bool isMoving;
        public bool isLocked;
        public bool isReady;
    }

    // create a instance
    private flags myFlag;

    public bool CheckReadiness()
    {
        if(this.myFlag.isMoving == true) return true;
        return false;
    }

    // Better Implementation
    public bool CheckReadiness()
    {
        if (this.myFlag.isMoving) return true;
        return false;
    }

    // Best Implementation
    public bool CheckReadiness()
    {
        return (this.myFlag.isMoving);
    }

Instead of creating the struct it might make more sense to have properties. 除了创建结构之外,拥有属性可能更有意义。

public class movContr
{
    int movId;

    public bool IsMoving { get; set; }
    public bool IsLocked { get; set; }
    public bool IsReady { get; set; }
        (...)
}

Then instead of calling CheckReadiness you'd just call IsReady . 然后,只需调用IsReady而不是调用CheckReadiness即可。

var temp = new movContr();
if(temp.IsReady)
{
    // It is ready.
}

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

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