How to pass variables to a class in c# -
i've simple code:
class resourceinterceptor: iresourceinterceptor { public bool onfilternavigation(navigationrequest request) { return false; } resourceresponse iresourceinterceptor.onrequest(resourcerequest request) { request.referrer = "http://www.google.com"; return null; } }
i need pass variable class (just read it, not edit), if insert variable not declared within class gives me error:
cannot access non-static member of outer type 'windowsformsapplication1.form1' via nested type 'windowsformsapplication1.form1.resourceinterceptor'
for example need somethin (obviously doesn't works!)
public string referrer = "www.google.com"; class resourceinterceptor: iresourceinterceptor { public bool onfilternavigation(navigationrequest request) { return false; } resourceresponse iresourceinterceptor.onrequest(resourcerequest request) { request.referrer = referrer; return null; } }
you can use initializing constructor. example:
class resourceinterceptor: iresourceinterceptor { public resourceinterceptor(string referer) { m_referer = referer; } public bool onfilternavigation(navigationrequest request) { return false; } resourceresponse iresourceinterceptor.onrequest(resourcerequest request) { request.referrer = m_referrer; return null; } private string m_referer; }
and pass referer constructor when create instance of class:
resourceinterceptor interceptor = new resourceinterceptor("www.google.com");
Comments
Post a Comment