How to abstract a property on ASP.NET pages

I use aspx forms. We do what we declare a property as for each form. The code is at the end

So, I thought that if we had a base class that has all such things, but since the form already inherits the page class, we don’t have multiple inheritance in C # so here we can use the interface to achieve this. I'm new to oops, please let me know if this is possible or not.

public partial class Default2 : System.Web.UI.Page
{
    public string ImagePath { get; set; }
}
+3
source share
2 answers

You have two options.

Create base class

You can create a base class that inherits System.Web.UI.Pageand inherits this class on all of your pages:

public class BasePage : System.Web.UI.Page
{
    public string ImagePath { get; set; }
}

And then inherit this class on your pages:

public partial class Default2 : BasePage
{
    ....
}

Create interface

:

public interface Interface1
{
    string ImagePath { get; set; }
}

:

public partial class Default2 : System.Web.UI.Page, Interface1
{
    public string ImagePath { get; set; }
}

, ( ) . . .

, , , . .

+5

. .

, :

public partial class MyPageBase : System.Web.UI.Page
{
    public string ImagePath { get; set; }
}

Then inherit all other classes from your base class. There you can access everything from Pageand MyPageBase:

public class MyPage : MyPageBase
{
    public string TestAccessBaseProp()
    {
        var foo = base.ImagePath;
    }

    public string SomeAdditionalProperty { get; set; }
}
+3
source

All Articles