tags:

views:

466

answers:

3

I want to have a abstract view for any type of UI (web or window). In order to do that I must use Interface (IView ) in which I can only apply just rules about view. In fact, I want to set a some basic comple function to provide to its inheritances.

So in this way, I must use abstract class. The problem is

1) Interface only have rules 2) The view (web form or window form) can't inherit any more since that's already inherited from window or web form

How can I do that? Many thanks

+3  A: 

You can inherit a concrete class (web form/window form), declare your class abstract, and still implement an interface.

System.Web.UI.Page example:

public interface IView
{
    void Foo();
}

public abstract class BasePage : Page, IView
{
    //honor the interface
    //but pass implementation responsibility to inheriting classes
    public abstract void Foo();

    //concrete method
    public void Bar()
    {
        //do concrete work
    }
}

public class ConcretePage : BasePage
{
   //implement Foo
    public override void Foo() { }
}

This gives you the benefit of the interface and the concrete method Bar();

Corbin March
+4  A: 

Will the functions you add change the definition of what the class is, or will you simply be creating functions to manipulate data that is already a part of the class?

If you need to redefine aspects of the base class of these two classes then yes, you will need to change your inheritance structure. But if the functions will simply manipulate data that is already part of the class now then I would suggest you use the interface and create utility functions.

Here is a clumsy example of what I mean:

using System;

abstract class Pet { }

class Dog : Pet, IPet
{
    public String Name { get; set; }
    public Int32 Age { get; set; }
}

class Cat : Pet, IPet
{
    public String Name { get; set; }
    public Int32 Age { get; set; }
}

interface IPet
{
    String Name { get; set; }
    Int32 Age { get; set; }
}

static class PetUtils
{
    public static void Print(this IPet pet)
    {
     Console.WriteLine(pet.Name + " is " + pet.Age);
    }
}

Your two UI classes are perhaps related in this way. I would imagine that what you need to do in a cross-cutting fashion would be solved by a utility method like the one I have created. I did create the PetUtils.Print method as an extension method as this will create the expressive illusion of an instance method. If you are not using C# 3 just remove "this" from public static void Print(this IPet pet).

Andrew Hare
A: 

what i want to do is something like this

Public MustInherit Class ControllerBase(Of M As {ModelBase, New}, V As {IView})

Inherits BaseController

Dim model As M
Dim view As V
Public Sub New()
    model = New M
    view.Controller = Me
    model.Controller = Me
End Sub
Public Overridable Function GetModel() As M
    Return model
End Function
Public Overridable Function GetView() As V
    Return view
End Function

End Class

Public Class BaseWindow

Inherits System.Windows.Forms.Form
Implements IView
Dim c As BaseController

Public Function GetController() As BaseController
    Return c
End Function

Friend WriteOnly Property Controller() As BaseController Implements IView.Controller
    Set(ByVal value As BaseController)
        c = value
    End Set
End Property

End Class

 Public Class BaseWeb
Inherits System.Web.UI.Page
Implements IView

Dim c As BaseController   

Public Function GetController() As BaseController
    Return c
End Function

Friend WriteOnly Property Controller() As BaseController Implements IView.Controller
    Set(ByVal value As BaseController)
        c = value
    End Set
End Property

End Class

Public Interface IView

WriteOnly Property Controller() As BaseController

End Interface

mtt