c# - How do I call a derived class method from the base class? -


i have read several similar questions none seem solve problem facing. typical answer cast derived class cannot since not know derived class type.

here example:

class wirelessdevice { // base class     void websocket.parsemessage(); // inserts data wireless device object }  class wifi : wirelessdevice { // derived class     gpsloc loc; } 

wireless device can derived make bluetooth, wi-max, cellular, etc. devices , not know type of wirelessdevice receiving data.

when gps packet received on websocket in base class need update derived device's location.

i thought perhaps sending message via queue or creating event handler , sending location in event arguments seem little clunky when data staying within class.

is there built language allow me call derived device base class without knowing type?

the correct way add method dosomemagic() in base class, default implementation, or abstract. derived class should override magic.

something maybe:

public class wirelessdevice { // base class     protected virtual void parsemessage()     {         // common stuff in here     } }  public class wifi : wirelessdevice { // derived class     override void parsemessage()     {         base.parsemessage();//call if need operations base impl.         dogpsstuff();     }     private void dogpsstuff()     {         //some gps stuff     }     gpsloc loc; } 

Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -