0

I have a code that looks like this:

using (DC dc = new DC())
{
    f(dc.obj, a);
}


void f(DC dc, int a)
{
    ...
    dc.obj = a;
}

It doesnt work - complains about object reference and non-static fields. This is a console application, so it has Main() function. How should I make it work? I tried adding references as it asked:

I have a code that looks like this:

using (DC dc = new DC())
{
    f(ref dc.obj, a);
}


void f(ref DC dc, int a)
{
    ...
    dc.obj = a;
}

but it still didnt work

2 Answers 2

3

This has nothing to do with the using statement. You are trying to call a non-static member function from Main, which is static. You cannot do that because 'f' is an instance method, i.e., you must call it on or from an instance of your Program class. So, you need to make your function f static.

Sign up to request clarification or add additional context in comments.

Comments

2

f is an instance method, presumably in the Program class, right? If you are calling f from Main, then there is no instance of Program, because Main is a static method. Change f to be static:

static void f(DC dc, int a) { ... }

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.