c# - Extending functionality through interfaces -
i have implemented interface iservice
inherits functionality series of other interfaces , serves common ground many different services.
each of these services being described interface, example:
public interface iserviceone : iservice { //... } public class serviceone : iserviceone { //... }
everything point works expected:
iserviceone serviceone = new serviceone(); iservicetwo servicetwo = new servicetwo();
what have add big list of constants (public variables) each of these services different per service type (for example, iserviceone
have different constants iservicetwo
, there constants in iserviceone
not exist in iservicetwo
, etc).
what i'm trying achieve that:
iserviceone serviceone = new serviceone(); var someconstantvalue = serviceone.const.someconstant;
just because variables differ of service type decided implement interface each of them:
public interface iserviceoneconstants { //... }
and broaden iservice
definition:
public interface iserviceone : iservice, iserviceoneconstants { //... } public class serviceone : iserviceone { //... }
the problem have don't know how implement concrete class iserviceoneconstants
. time 1 of variables (we called them constants here) called has instantiated, though of static
class cannot expose static
class's functionality through interface. tried singleton
, expose instance
via public non-static wrapper:
public class singleton : iserviceoneconstants { private static singleton _instance; private singleton() { someconstant = "some value"; } public static singleton instance { { if (_instance == null) { _instance = new singleton(); } return _instance; } } public string someconstant { get; set; } public singleton const { { return instance; } } }
i adjusted iserviceoneconstants
that:
public interface iserviceoneconstants { singleton const { get; } }
but when call this:
iserviceone serviceone = new serviceone(); var someconstantvalue = serviceone.const.someconstant;
i null reference
exception, .const
null.
what missing here?
you helped confused possible, naming different stuff same name ;)
so, first... you're trying access singleton instance through instance property:
public singleton const { { return instance; } }
then using like:
serviceone.const
but variable never assigned. in order assign it, should make instance of singleton class, assign serviceone.const property , might use it.
what need this:
public class serviceone : iserviceone { public singleton const { { return singleton.instance; } } }
Comments
Post a Comment