I am having problems with the code below. It is writen in C# on .Net 3.5. The actual code is much more lengthy, but I copied what should be important. The error I keep generating in VS is "An object reference is required for the non-static field, method, or property" with each of the DoCommand sections. I'm hoping this is an easy solution with a more seasoned set of eyes.

Thanks for any input.

namespace Sample
{
    public class Sample : SampleBase
    {
        public Sample(IExecutionContext context)
            : base(context)
        {
        }

        public override string CallFunc(object sender, string function,
                                         object args, out object ret)
        {

            RunSequence();

            ret = null;
            return "Ret";
        }

        static void RunSequence()
        {
            long actionNumber = 123456789;

            DoCommand(OpsCommandType.Action, (long)actionNumber);
        }

        private void DoCommand(OpsCommandType command, long number)
        {
            var opsCommand = new OpsCommand();
            opsCommand.Command = command;
            opsCommand.Number = number;
            DoCommand(opsCommand);
        }

        private void DoCommand(OpsCommand opsCommand)
        {
            var status = OpsContext.ProcessCommand(opsCommand);
            if (!status.IsSuccess)
                MessageBox.Show("ERROR");
        }

        public override void Destroy()
        {
            base.Destroy();
        }
    }

}

Recommended Answers

All 3 Replies

Line 21 you declare that method static, then try to call a non-static method (DoCommand). Non-static methods require that you create an object of the class first so you have something that actually has the method.

You can make all the DoCommand() methods static or you can create an object in the static method to call:

static void RunSequence() {
    long actionNumber = 123456789;

    Sample mySample = new Sample();
    mySample.DoCommand(OpsCommandType.Action, (long)actionNumber);
}

Line 21 you declare that method static, then try to call a non-static method (DoCommand). Non-static methods require that you create an object of the class first so you have something that actually has the method.

You can make all the DoCommand() methods static or you can create an object in the static method to call:

static void RunSequence() {
    long actionNumber = 123456789;

    Sample mySample = new Sample();
    mySample.DoCommand(OpsCommandType.Action, (long)actionNumber);
}

I had tried that earlier, but was never able to get past another error that it doesn't contain a constructor that takes 0 arguments. If someone can point me in the direction to fix that, I'd be happy to continue down that path, but I think I'm a little lost.

If I change static void RunSequence() to private void RunSequence() I'm only left with one error on var status = OpsContext.ProcessCommand(opsCommand); .

private void DoCommand(OpsCommand opsCommand)
        {
            var status = OpsContext.ProcessCommand(opsCommand);
            if (!status.IsSuccess)
                MessageBox.Show("ERROR");
        }

Is it possible that the problem is with the way OpsContext.ProcessCommand is definded in the separate DLL (which I can't change)? If so, what should I look at to fix it?

And that error would be?

As for the constructor problem. The system creates a default constructor that takes zero arguments. If you create a constructor that has arguments, it removes the constructor it created. So you'd need something like:

public Sample() : base() {}

. Of course your base object may not have a zero parameter constructor either :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.