Andrew Simpson
Andrew Simpson

Reputation: 7324

How to use abstract classes inside a UserControl

I have a UserControl.

I want each UserControl to Override an Abstract method.

This is my abstract class:

public class MyAbstract
{
    public virtual void LoadData()
    {

    }
}

This my usercontrol with my latest attempt at getting this to work:

public partial class ucAbstract : UserControl, MyAbstract
{
    public ucAbstract()
    {
        InitializeComponent();
    }

    public override void LoadData()
    {
        base.Load();
        {

        }
    }
}

The error is:

Class 'ucAbstract' cannot have multiple base classes: 'UserControl' and 'MyAbstract'

How can I do this?

ADDITIONAL: I may have to remove this addition and create a new question.

This is what I am trying to achieve:

My main form contains 2 UserControls: ucOne, ucTwo

Both of these controls have a method called 'LoadData'.

I have a function in my main form:

void LoadControl(iuserControl myUserControl)
{
    myUserControl.LoadData();
}

Upvotes: 2

Views: 3486

Answers (4)

Gopichandar
Gopichandar

Reputation: 2832

How about this.

Create a Base class that contains all the common methods of your UserControl. Make sure that it is extended with UserControl class

MyAbstract.cs

public abstract class MyAbstract   : UserControl
{
    public virtual void LoadData()
    {

    }
}

then create a UserControl and extend that with MyAbstract class. You can use like this.

ucAbstract.xaml.cs

public partial class ucAbstract : MyAbstract
{
    public ucAbstract()
    {
        InitializeComponent();
    }

    public override void LoadData()
    {
        base.LoadData();
        {

        }
    }
}

Also, you need to have <local:MyAbstract> instead of <UserControl> in the xaml

ucAbstract.xaml

<local:MyAbstract x:Class="YourNamespace.ucAbstract"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:YourNamespace"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <StackPanel>
        <TextBox />
    </StackPanel>
</local:MyAbstract> 

Note:

  • You can create multiple UserControls with MyAbstract as BaseClass.
  • UserControls only allow one level of inheritance, at least if the MyAbstract has a XAML this surely will not work.

Reference: Partial declarations, must not specify different base classes

Upvotes: 4

Henka Programmer
Henka Programmer

Reputation: 747

You can Create your UserControl

Public class MyUserControl : UserControl
{
public virtual LoadData()
{
// ...
}
}

Upvotes: 0

AnjumSKhan
AnjumSKhan

Reputation: 9827

You actually need ICommand object with proper CommandParameter for conditional loading, in place of your LoadData() method.

Upvotes: 0

Salah Akbari
Salah Akbari

Reputation: 39956

C# doesn't support multiple inheritance and you cannot inherit from multiple classes (UserControl and MyAbstract). You could use interface instead of class like this:

public interface IMyAbstract
{
    void LoadData();
}

Then you should implement the interface like this:

public partial class ucAbstract : UserControl, IMyAbstract
{
   public void LoadData()
   {

   }
}

Upvotes: 2

Related Questions