简体   繁体   English

C # 结构私有/公共错误

[英]C # Struct private/public error

I am having problems with my Struct:), the compiler gives me the error我的结构有问题:),编译器给了我错误

"An object reference is required for the non-static field, method, or property 'SonyasProgram.Form1.frameLimits.Max.Y.get'" “非静态字段、方法或属性‘SonyasProgram.Form1.frameLimits.Max.Y.get’需要 object 引用”

I have tried looking for a solution but none par particularly clear.我曾尝试寻找解决方案,但没有一个特别清楚。 Here is my code:这是我的代码:

    public partial class Form1 : Form
{
    public struct frameLimits
    {
        public struct Max
        {
            private int yval = 220;
            public int Y
            {
                get
                {
                    return yval;
                }
                set
                {
                    yval = value;
                }
            }
        }
    }

    public Form1()
    {
        frameLimits.Max.Y = 1;

The last line is where the error is identified.最后一行是识别错误的地方。 Any help would be awesome, thanks !任何帮助都会很棒,谢谢!

Just as the compiler is telling you, you're trying to perform an operation that needs a static variable.正如编译器告诉您的那样,您正在尝试执行需要 static 变量的操作。 You either want to create an instance of frameLimits, or more likely, declare yval as static .您要么想要创建 frameLimits 的实例,要么更有可能将yval声明为static

frameLimits and Max are types not struct instances - you can only set instance properties of an instance. frameLimitsMax类型而不是struct实例- 您只能设置实例的实例属性。 Besides that you should really not use mutable, nested structs - please reconsider.除此之外,您真的不应该使用可变的嵌套结构 - 请重新考虑。

Problem:问题:

(followed by Hacked Solution , followed by Simplified Solution ) (其次是黑客解决方案,其次是简化解决方案

Currently you have two data types declared and one is nested, so you can declare variables using each of the data types as such:目前,您声明了两种数据类型,一种是嵌套的,因此您可以使用每种数据类型声明变量,如下所示:

frameLimits.Max highest = new frameLimits.Max();
highest.Y = 5;

IntelliSense shows: highest. IntelliSense 显示: highest.在此处输入图像描述

frameLimits limits = new frameLimits();
// no members exist on the frameLimits type, so nothing to access. 

IntelliSense shows: limits. IntelliSense 显示: limits.在此处输入图像描述

IntelliSense can help sort it out. IntelliSense 可以帮助解决它。

The nested structure isn't usable in the way you originally attempted because as others have already indicated before me, you're trying to use the types instead of instances of those types.嵌套结构不能以您最初尝试的方式使用,因为正如其他人已经在我之前指出的那样,您正在尝试使用类型而不是这些类型的实例。

Hacked Solution黑客解决方案

If I add an instance field called MaxValue to your Max struct, then I might be getting close to what you intended.如果我将一个名为 MaxValue 的实例字段添加到您的 Max 结构中,那么我可能会接近您的意图。 This will enable the caller code to be:这将使调用者代码成为:

            frameLimits limits = new frameLimits();
            limits.MaxValue.Y = 5;

However that's a really convoluted way to do it and not recommended.然而,这是一种非常复杂的方式,不推荐。

Modified struct is this:修改后的结构是这样的:

public struct frameLimits
{
    public struct Max
    {
        private int yval;
        public int Y
        {
            get
            {
                return yval;
            }
            set
            {
                yval = value;
            }
        }
    }

    public Max MaxValue;
}

A Simplified Solution简化的解决方案

You could remove the inner struct to simply things, however you're still under some restrictions with the struct and that's why I implement the default value of 220 as I did.您可以将内部结构删除为简单的事情,但是您仍然受到结构的一些限制,这就是为什么我像我一样实现默认值 220 的原因。

The new caller code is:新的调用者代码是:

            FrameLimits limits = new FrameLimits();
            Console.Write( limits.MaxY );  // prints 220
            limits.MaxY = 5;

This struct is simplified to a degree (removed nested struct):这个结构在一定程度上被简化(移除了嵌套结构):

public struct FrameLimits
{
    private int? yval;

    public int MaxY
    {
        get
        {
            return yval.HasValue ? yval.Value : 220;
        }
        set
        {
            yval = value;
        }
    }
}

frameLimits is a struct (think type), so you need to either create an instance first in order to your assignment, or make the path to the field to only contain static members. frameLimits 是一个结构(认为类型),因此您需要先创建一个实例才能进行分配,或者使该字段的路径仅包含 static 成员。

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

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