简体   繁体   中英

Event not firing

Can someone explain what is wrong with this code? This event will not fire when I make a call to mco.

    private ModiconComunications.ModiconComObject withEventsField_mco = new ModiconComunications.ModiconComObject();
    private ModiconComunications.ModiconComObject mco
    {
        get { return withEventsField_mco; }
        set
        {
            if (withEventsField_mco != null)
            {
                withEventsField_mco.GetDataReturn -= mco_GetDataReturn;
            }
            withEventsField_mco = value;
            if (withEventsField_mco != null)
            {
                withEventsField_mco.GetDataReturn += mco_GetDataReturn;
            }
        }
    }

When I call the below line, it should fire off the event mco_GetDataReturn, however, it does not. What have I done incorrectly?

    mco.GetData(ModiconComunications.ModiconComObject.GetDataType.READ_MODICON_HREGS, 11421, 9, 0);

Well, you initial withEventsField_mco, the one that is created by this line:

private ModiconComunications.ModiconComObject withEventsField_mco = new ModiconComunications.ModiconComObject();

Doesn't get it's event handlers hooked up. Unless you are doing that in the constructor.

A little more code would be helpfull.

You init withEventsField_mco with a new object. However since you don't use the setter on the mco property no events get wired.

try this to test this reasonig:

McocontainingObject.mco = new ModiconComunications.ModiconComObject();
mco.GetData(ModiconComunications.ModiconComObject.GetDataType.READ_MODICON_HREGS, 11421, 9, 0);

or in your constructor

class McoContainer{
   private ModiconComunications.ModiconComObject withEventsField_mco;

   public McoContainer(){
      this.mco = new ModiconComunications.ModiconComObject();
   }

   public ModiconComunications.ModiconComObject mco{
      get{...}
      set{...}
   }
}

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