c# - Generic object for two different classes -
i have class acquisition device. want create class generates random samples when acquisition device not connected.
this object:
private object amplifierobj;
and want create that
if (ampsettingsobj.daqdevice == "noamp") ampobj = new noampmanager(samplerate: samplerate); else ampobj = new usbampmanager(optcalibrationflag: calibrationflag, serialnumbers: serialnumbers, samplerate: samplerate);
however, when call 1 of methods error "object" not contain definition method . both classes have same methods implemented. best way of implementing it? should use generic class placeholder?
if both classes have same methods should have interface (iamplifier) , both classes should implement interface.
this can done right clicking 1 of classes , selecting refactor / extract interface.
assuming interface name iamplifier, have both classes implement same interface such as:
public class noampmanager : iamplifier { ... (methods) ... (properties) } public class usbampmanager : iamplifier { ... (methods) ... (properties) }
then, instead of
private object amplifierobj;
use
private iamplifier amplifierobj;
Comments
Post a Comment