Reputation: 604
Hello I want to access an object from inheritance in my project,but I cant find a way My page is ;
public partial class siteler_page : siteDynamic
{
public static string pageType = "contentpage";
protected void Page_Load(object sender, EventArgs e)
{
}
}
And the main class is ; (i want to access that pageType paramter in onpreinit)
public class siteDynamic : System.Web.UI.Page
{
public siteDynamic()
{
//
// TODO: Add constructor logic here
//
}
protected override void OnPreInit(EventArgs e)
{
// I want to access the pageType in here
base.OnPreInit(e);
}
}
Any help is appreciated,thans
Upvotes: 2
Views: 82
Reputation: 2021
this will not work because your parent class siteDynamic is called an initialized first before your child class siteler_page. At least this is how you set it up. In order for this to work set your parent class should have a property in the parent class then override the base class method and set the value there.
public abstract class siteDynamic : System.Web.UI.Page
{
public string PageType { get; set; }
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
}
}
public partial class siteler_page : siteDynamic
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected override void OnPreInit(EventArgs e)
{
base.PageType = "contentpage";
base.OnPreInit(e);
}
}
Upvotes: 1
Reputation: 26281
One way to do it is to define an abstract property and let the child classes override it (siteDynamic
should be an abstract
class):
public abstract class siteDynamic : System.Web.UI.Page
{
public siteDynamic()
{
// ...
}
public abstract string PageType { get; }
protected override void OnPreInit(EventArgs e)
{
string type = this.PageType;
// ...
base.OnPreInit(e);
}
}
public partial class siteler_page : siteDynamic
{
public override string PageType
{
get
{
return "contentpage";
}
}
protected void Page_Load(object sender, EventArgs e)
{
// ...
}
}
Upvotes: 1