Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to abstract a property in ASP.NET pages

Tags:

c#

asp.net

I am using aspx forms. What we do is that we declare property like for every form. Code is at the end

So I was thinking that if we could have a base class that have all the stuff like this but as the form already inherit the Page class we don't have multiple inheritance in C# so here can we use interface to achieve this I am new to oops please let me know it is possible or not.

public partial class Default2 : System.Web.UI.Page
{
    public string ImagePath { get; set; }
}
like image 931
Danish Subhu Avatar asked Dec 04 '25 16:12

Danish Subhu


1 Answers

You have two options.

Create a base class

You can create a base class that inherits System.Web.UI.Page and inherit this class in all your pages:

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

And then inherit this class in your pages:

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

Create an interface

You can create an interface like this:

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

And then inherit this interface on all your pages:

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

As you can see, the first option (base class) has some advantages over the interface option. You have to define and implement your property only once when using a base class. When using interfaces you have to implement the property on every page too.

This is because an interface is only used to define what properties and function there are on an object, not how they work. While a base class can already implement the properties and functions.

like image 165
RononDex Avatar answered Dec 07 '25 06:12

RononDex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!