简体   繁体   中英

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'"

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. You either want to create an instance of frameLimits, or more likely, declare yval as static .

frameLimits and Max are types not struct instances - you can only set instance properties of an instance. 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. 在此处输入图像描述

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

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

IntelliSense can help sort it out.

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. 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.

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.

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