Help fixing errors in C# “A ref or out argument mu

2019-01-29 13:40发布

问题:

Here's a block of code I'm having errors with:

    public static bool Flash(Form form)
    {
        return (Win2000OrLater && FlashWindowEx(ref Create_FLASHWINFO(form.Handle, 15, uint.MaxValue, 0))); //A ref or out argument must be an assignable variable

    }

    public static bool Flash(Form form, uint count)
    {
        return (Win2000OrLater && FlashWindowEx(ref Create_FLASHWINFO(form.Handle, 3, count, 0))); //A ref or out argument must be an assignable variable
    }
    private static extern bool FlashWindowEx(ref FLASHWINFO pwfi);
    public static bool Start(Form form)
    {
        return (Win2000OrLater && FlashWindowEx(ref Create_FLASHWINFO(form.Handle, 3, uint.MaxValue, 0))); //A ref or out argument must be an assignable variable
    }

    public static bool Stop(Form form)
    {
        return (Win2000OrLater && FlashWindowEx(ref Create_FLASHWINFO(form.Handle, 0, uint.MaxValue, 0))); //A ref or out argument must be an assignable variable
    }

    private static bool Win2000OrLater
    {
        get
        {
            return (Environment.OSVersion.Version.Major >= 5);
        }
    }

The error message is:

A ref or out argument must be an assignable

回答1:

Regarding your first errors, you need to introduct the FlashWindow as a variable so for example

This:

public static bool Flash(Form form)
{
    return (Win2000OrLater && FlashWindowEx(ref Create_FLASHWINFO(form.Handle, 15, uint.MaxValue, 0))); //A ref or out argument must be an assignable variable
}

becomes:

public static bool Flash(Form form)
{
    if (Win2000OrLater)
    {
        FLASHWINFO fi = Create_FLASHWINFO(form.Handle, 15, uint.MaxValue, 0);
        return (FlashWindowEx(ref fi));
    }
    return false;
}


回答2:

You need an assignable variable for a ref or out param.

FLASHWINFO fwi = Create_FLASHWINFO(form.Handle, 15, uint.MaxValue, 0);
FlashWindowEx(ref fwi);