2

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();
}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
Andrew Simpson
  • 6,883
  • 11
  • 79
  • 179
  • 1
    MyAbstract needs to iherit `UserControl`. You can't inherit multiple classes at the same time in C#. You have to chain the inheritance in a hierarchical way. Sorry if there's a better term to use. – TyCobb Mar 02 '16 at 17:56
  • 1
    C# does not have multiple inheritance – d.moncada Mar 02 '16 at 17:56
  • `MyAbstract` *isn't* `abstract`, and your `ucAbstract` class is attempting to inherit from two base class, which isn't possible in C#. – Preston Guillot Mar 02 '16 at 17:56
  • @TyCobb Thanks. I understand what you mean – Andrew Simpson Mar 02 '16 at 17:59
  • @d.moncada thank you – Andrew Simpson Mar 02 '16 at 17:59
  • @PrestonGuillot I though just by prefixing the method with 'virtual' it makes it abstract? Should I declare the class as abstract then? thanks – Andrew Simpson Mar 02 '16 at 18:00
  • `virtual` just means you can override it, but also have code in the base body. `abstract` means you just have a signature and the inheritor *has* to override it. – TyCobb Mar 02 '16 at 18:02
  • 1
    Virtual and abstract don't mean the same thing. An `abstract` class is one that cannot be instantiated directly, and *may* have `abstract` members. Any class deriving from an `abstract` class *must* override any `abstract` members of its base class. Concrete classes can have `virtual` members, which inheriting classes may *optionally* override. – Preston Guillot Mar 02 '16 at 18:03
  • @bothOfYou shows how useless i am. I thought it was the other way round - thanks – Andrew Simpson Mar 02 '16 at 18:03
  • @AndrewSimpson . . See my answer below. Hope it will help you. . – Gopichandar Mar 03 '16 at 06:51

4 Answers4

4

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

Community
  • 1
  • 1
Gopichandar
  • 2,742
  • 2
  • 24
  • 54
  • 1
    Hi, thanks for that. i was almost there in 1 of my attempts. I had done as you had said but I had not marked the class as abstract - also thanks for link – Andrew Simpson Mar 03 '16 at 08:04
2

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()
   {

   }
}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
0

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

AnjumSKhan
  • 9,647
  • 1
  • 26
  • 38
0

You can Create your UserControl

Public class MyUserControl : UserControl
{
public virtual LoadData()
{
// ...
}
}
Henka Programmer
  • 727
  • 7
  • 25