C#: "field is not assigned to" warning

Talk about things C#, some related to AutoHotkey
Miguel7
Posts: 186
Joined: 08 Sep 2014, 07:06

C#: "field is not assigned to" warning

Post by Miguel7 » 07 Sep 2017, 14:45

Hey guys, newbie question here: I've got a class with a property that is an instance of another class. That other class has no parameters in its constructor (and really, no constructor at all) but I'm getting this ridonkulous message saying the property is never assigned too, will always be null, and Kiss My Bits. :P

Here's the code:

Code: Select all

// In one class, I have this:
internal class WindowsInputMessageDispatcher
    {
        /// <summary>
        /// Dispatches the specified list of <see cref="INPUT"/> messages in their specified order by issuing a single called to <see cref="NativeMethods.SendInput"/>.
        /// </summary>
        /// <param name="inputs">The list of <see cref="INPUT"/> messages to be dispatched.</param>
        /// <exception cref="ArgumentException">If the <paramref name="inputs"/> array is empty.</exception>
        /// <exception cref="ArgumentNullException">If the <paramref name="inputs"/> array is null.</exception>
        /// <exception cref="Exception">If the any of the commands in the <paramref name="inputs"/> array could not be sent successfully.</exception>
        public void DispatchInput(INPUT[] inputs)
        {
            if (inputs == null) throw new ArgumentNullException("inputs");
            if (inputs.Length == 0) throw new ArgumentException("The input array was empty", "inputs");
            var successful = NativeMethods.SendInput((UInt32)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
            if (successful != inputs.Length)
                throw new Exception("Some simulated input commands were not sent successfully. The most common reason for this happening are the security features of Windows including User Interface Privacy Isolation (UIPI). Your application can only send commands to applications of the same or lower elevation. Similarly certain commands are restricted to Accessibility/UIAutomation applications. Refer to the project home page and the code samples for more information.");
        }
    }

// Inside a public class that uses it, here's the code that gives the compiler Hulk out and makes my hair loss progress complete:
private static WindowsInputMessageDispatcher _messageDispatcher = new WindowsInputMessageDispatcher();

// Then of course I try to reproduce it several ways, and it's fine - here's one example:
    internal class A
    {
        int DoSomething(int x) { return x * 5; };
    }

    public class B
    {
        public A a = new A();
    }
My best guess is that it has something to do with access modifiers ("internal" vs. "public") but that's not the error I'm getting. And to add to the mystery, the error makes no sense whatsoever - at least, not from my (admittedly human) perspective; obviously there's a blatant mistake in the Oz-like world the compiler lives in. Never assigned to? Isn't the NEW on the SAME LINE assigning it??? I must be missing something stupidly simple, because hair-jerkers like this are never half as complicated as they seem.

Return to “C#”