Search code examples
c#windowswinformsref

A ref or out argument must be an assignable variable?


Error:

A ref or out argument must be an assignable variable

Code:

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

public class OAKListView : ListView
{
    protected override void OnHandleCreated(EventArgs e)
    {
        base.OnHandleCreated(e);

        this.WndProc(ref new Message()
        {
            HWnd = this.Handle,
            Msg = 4150,
            LParam = (IntPtr)43,
            WParam = IntPtr.Zero
        });
    }
}

It shows error

this.WndProc(ref new Message()


Solution

  • The error explains it clearly. You need an assignable variable

    protected override void OnHandleCreated(EventArgs e)
    {
        base.OnHandleCreated(e);
        var message = new Message()
        {
            HWnd = this.Handle,
            Msg = 4150,
            LParam = (IntPtr)43,
            WParam = IntPtr.Zero
        };
        this.WndProc(ref message);
    }