jeremychan
jeremychan

Reputation: 4459

object reference compiling error

private static void runantc_OutputDataReceived(object sendingProcess, DataReceivedEventArgs outLine)  
        {
            // Collect the sort command output.
            if (!String.IsNullOrEmpty(outLine.Data))
            {
                ProcoutputTextBlock.Dispatcher.BeginInvoke(new Action(() => { ProcoutputTextBlock.Text += outLine.Data; }, null));
            }
        }

Hi, in the above code i am having errors @ProcoutputTextBlock Error 1 An object reference is required for the non-static field, method, or property 'WpfApplication1.Window1.ProcoutputTextBlock'

and @ () => { ProcoutputTextBlock.Text += outLine.Data; }, null Error 2 Method name expected

can anybody enlighten me?

Upvotes: 0

Views: 103

Answers (2)

Adam Robinson
Adam Robinson

Reputation: 185593

Your function is static, which means that it requires a reference to an instance of the class in order to access any instance members. Since I'm assuming that ProcoutputTextBlock is a TextBlock on your window, it needs an instance in order to access it.

The other option is making the function non-static, but since you don't show how this event handler is being attached, I don't know if that's a viable option for you.

Upvotes: 2

Alex Aza
Alex Aza

Reputation: 78447

ProcoutputTextBlock is an instance property. You are accessing instance property from static method runantc_OutputDataReceived.

To resolve this remove static from runantc_OutputDataReceived declaration.

To fix the second issue, remove the second argument null. Like this:

ProcoutputTextBlock.Dispatcher.BeginInvoke(new Action(() => { ProcoutputTextBlock.Text += outLine.Data; }));

or

ProcoutputTextBlock.Dispatcher.BeginInvoke((Action)(() => { ProcoutputTextBlock.Text += outLine.Data; }));

Upvotes: 1

Related Questions