Search code examples
c#methodsstatic-methodsintptr

Why can I not use a value inside a IntPtr method?


Besides the wrong use of DLLs, when I try to use the theTestValue inside the IntPtr method the IntelliSense marks it as fail. I would like to know why this is happening because I need to use a bool from outside inside this method.

public partial class Form1 : Form
{

    [DllImport("user32.dll")]
    private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
    IntPtr wParam, IntPtr lParam);

    [DllImport("user32.dll")]
    private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
    IntPtr wParam, IntPtr lParam);

    private static LowLevelKeyboardProc _proc = HookCallback;
    private delegate IntPtr LowLevelKeyboardProc(
    int nCode, IntPtr wParam, IntPtr lParam);

    public bool theTestValue = false; //This is the value

    private static IntPtr HookCallback(
        int nCode, IntPtr wParam, IntPtr lParam)
    {
        theTestValue = true; //Red marked

        }
        return CallNextHookEx(_hookID, nCode, wParam, lParam);
    }

Solution

  • You cannot access the field because the method is static and the field has been declared on instance level (not static). If you change the code so that both are either static or not, the error will be gone.