user1548103
user1548103

Reputation: 869

Implementing an interface with a Windows Form

I'm new to using interfaces so I have a question that will probably be pretty easy for most of you.

I am currently trying to make an interface for a windows form. It looks something like

interface myInterface
{
    //stuff stuff stuff
}

public partial class myClass : Form, myInterface
{
   //More stuff stuff stuff. This is the form
}

The problem comes when I try to implement it. If I implement with

myInterface blah = new myClass();
blah.ShowDialog();

the ShowDialog() function is now available to it. It makes sense- myInterface is an interface, not a form... but I'm curious how I should go about implementing the interface with a windows form, or if it is even a viable option at all.

Does anyone have any suggestions about how I should go about doing that?

Thanks!

Upvotes: 6

Views: 18477

Answers (4)

dazedandconfused
dazedandconfused

Reputation: 3186

You can only access the items exposed by the type you use to hold myClass. For example,

Form f = new MyClass();
f.ShowDialog();  // Will work because f is of type Form, which has a ShowDialog method
f.stuff(); // Works because MyClass implements myInterface which exposes stuff()

All the things you want are in there, but you have to reference them differently than you're trying to.

Upvotes: 0

Sayse
Sayse

Reputation: 43320

This appears to be a question about how to correctly expose members of a class.

internal - Access to a method/class is restricted to the application
public - Access is not restricted
private - Access is restricted to the current class (methods)
protected - Access is restricted to the current class and its inherited classes

An example use of an interface is to share common method signatures between classes

interface IAnimal
{
    int FeetCount();
}
public class Dog : IAnimal
{
    int FeetCount()
    {
    }
}

public class Duck : IAnimal
{
    int FeetCount()
    {
    }
}

Upvotes: 1

mbeckish
mbeckish

Reputation: 10579

interface MyInterface
{
    void Foo();
}

public partial class MyClass : Form, MyInterface
{
   //More stuff stuff stuff. This is the form
}

Form f = new MyClass();
f.ShowDialog(); // Works because MyClass implements Form
f.Foo(); // Works because MyClass implements MyInterface

Upvotes: 2

RQDQ
RQDQ

Reputation: 15579

One way to approach this would be to add ShowDialog to myInterface:

 interface myInterface
 {
     DialogResult ShowDialog();
 }

Now, you can call that method on the interface without having to cast.

If you want to get a little more fancy with it, you could create another interface which represents any dialog...

interface IDialog
{
     DialogResult ShowDialog();
}

Then make your other interface implement IDialog:

interface myInterface : IDialog
{
     //stuff stuff stuff
}

This has the advantage of potentially reusing more code... You can have methods which accept an argument of type IDialog and they don't have to know about myInterface. If you implement a common base interface for all of your dialogs, you can treat the the same way:

void DialogHelperMethod(IDialog dialog)
{
     dialog.ShowDialog();
}

myInterface foo = new myClass();
DialogHelperMethod(foo);

Upvotes: 3

Related Questions