【问题标题】:A ref or out argument must be an assignable variable? [duplicate]ref 或 out 参数必须是可分配的变量? [复制]
【发布时间】:2018-12-24 11:01:44
【问题描述】:

错误:

ref 或 out 参数必须是可赋值变量

代码:

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
        });
    }
}

显示错误

this.WndProc(ref new Message()

【问题讨论】:

  • 是的。如果你这样通过,方法不会改变!
  • 我该如何解决这个问题?
  • 先创建一个Message()对象,然后发送给WndProc。
  • @Dolubolu 您可以将其分配给变量,但这可能是错误的。如果该方法采用ref 参数,它会更改它- 它说:如果您编写了该方法- 修复它!如果不是,请检查文档 - 也许你错了......

标签: c# windows winforms ref


【解决方案1】:

错误解释得很清楚。你需要一个可赋值的变量

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);
}

【讨论】:

    【解决方案2】:

    您的 ref 参数不是可分配的变量。不应同时创建Message 类的新实例并作为引用传递。调用方法应该填充内存中的某个位置。你的电话里没有这样的事情。这将编译:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-31
      • 2011-02-25
      • 2018-12-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多