简体   繁体   中英

EventArgs does not contain a constructor that takes 1 argument

I can't find a specific problem that matches mine, which I think has to do with the fact that I am creating a subclass of EventArgs with one argument, a string. When I try to compile, it seems to tell me that ScanInfoEventArgs doesn't have one constructor, when it clearly does (clearly to me at least).

I've only included the bit of code that I think applies. It seems like such a simple thing that I am at a loss.

 public partial class MainWindow : Window
{
    Coffee coffeeOnHand;
    SweetTea sweetTeaOnHand;
    BlueberryMuffin blueberryMuffinOnHand;

    public MainWindow()
    {
        InitializeComponent();

        //The following reads the inventory from file, and assigns each inventory item to the Coffee, SweatTea 
        //and BlueberryMuffin objects in memory.
        using (Stream input = File.OpenRead("inventory.dat"))
        {
            BinaryFormatter formatter = new BinaryFormatter();
            coffeeOnHand = (Coffee)formatter.Deserialize(input);
            sweetTeaOnHand = (SweetTea)formatter.Deserialize(input);
            blueberryMuffinOnHand = (BlueberryMuffin)formatter.Deserialize(input);
        }

        //The following adds whatever information is loaded from the objects on file from above
        //into the dropdown box in the menu.
        SelectedItemDropdown.Items.Add(coffeeOnHand);
        SelectedItemDropdown.Items.Add(sweetTeaOnHand);
        SelectedItemDropdown.Items.Add(blueberryMuffinOnHand);
    }

    public class ScanInfoEventArgs : EventArgs
    {
        ScanInfoEventArgs(string scanType)
        {
            this.scanType = scanType;
        }
        public readonly string scanType;
    }

    public class Scan
    {
        //Delegate that subscribers must implement
        public delegate void ScanHandler (object scan, ScanInfoEventArgs scanInfo);

        //The event that will be published
        public event ScanHandler onScan;

        public void Run()
        {
            //The ScanInfoEventArgs object that will be passed to the subscriber.
            ScanInfoEventArgs scanInformation = new ScanInfoEventArgs("scanType");

            // Check to see if anyone is subscribed to this event.
            if (onScan != null)
            {
                onScan(this, scanInformation);
            }
        }
    }

You need to make your constructor public . All class members default to private , which means the outside world can't get at them.

Since the compiler didn't see a matching public constructor (as in, one the code could actually invoke), it threw the error you saw.

Correct code:

    public ScanInfoEventArgs(string scanType)
    {
        this.scanType = scanType;
    }

Note that internal would work as well if all code resides in the same assembly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM