2

I have the following in C

typedef void (*procfunc)(V2fT2f *, float);

typedef struct {
    procfunc func;
    procfunc degen;
} Filter;

const Filter filter[] = {
    { brightness             },
    { contrast               },
    { extrapolate, greyscale },
    { hue                    },
    { extrapolate, blur      }, // The blur could be exaggerated by downsampling to half size
};

which I have brought over to C# to give me this

public delegate void procfunc(ImagingDefs.V2fT2f[] quad,float t);

    public class Filter
    {
        public procfunc func;
        public procfunc degen;
    };

    public Filter[] filter = new Filter[]
    {
        new Filter { func = brightness },
        new Filter { func = contrast },
        new Filter { func = extrapolate, degen = greyscale },
        new Filter { func = hue },
        new Filter { func = extrapolate, degen = blur } // The blur could be exaggerated by downsampling to half size
    };

My issue is that I'm getting the error

A field initializer cannot reference the nonstatic field, method or property.

I have a feeling that the issue is with the delegate, but I'm not sure - I've never needed to port this sort of code before.

Neither ImagingDefs nor V2fT2f are declared as static

The methods being called are typically

public void foo(ImagingDefs.V2fT2f[]quad, float t)

with nothing anywhere being static.

The name V2fT2f comes from the original source code

4
  • 1
    Like the error states, who is static and who is not? Show 1 or more of those methods too. Commented Aug 11, 2014 at 17:56
  • public void brightness(ImagingDefs.V2fT2f[]quad, float t) // t = 0 to 2 - nothing anywhere is declared as static Commented Aug 11, 2014 at 18:02
  • V2fT2f is not a particularly descriptive type name. I'd highly suggest using a more meaningful name. Commented Aug 11, 2014 at 18:11
  • Don't add clarifcations as comments, edit the question. Commented Aug 11, 2014 at 18:23

1 Answer 1

2

A field initializer cannot reference the nonstatic field, method or property. is referring to the filter methods such as brightness.

These methods must be made static if they're going to be referenced in your field initializer filter.

Another option is to initialize this field in the constructor of the class you're defining.

E.g.

public class MyNewClass
{
    public delegate void procfunc(ImagingDefs.V2fT2f[] quad,float t);

    public class Filter
    {
        public procfunc func;
        public procfunc degen;
    };

    public Filter[] filter;

    public MyNewClass()
    {
        filter = new Filter[]
        {
            new Filter { func = brightness },
            new Filter { func = contrast },
            new Filter { func = extrapolate, degen = greyscale },
            new Filter { func = hue },
            new Filter { func = extrapolate, degen = blur } // The blur could be exaggerated by downsampling to half size
        };
    }   
}
Sign up to request clarification or add additional context in comments.

Comments

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.