Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all - I have a question regarding how to use an interface in a form that is already open.

I have a C# project with two forms in it, A and B.

I have an interface X in a separate class, and B implements the interface methods, which update certain text boxes in B.

My question is: how do I call the interface from A without creating a new instance of B?

For example, if I do this in A:

C#
frmB xxx = new frmB();
xxx.PerformInterfaceMethod();
xxx.Show();


then of course a new instance of B pops up, with the actions defined by PerformInterfaceMethod() causing some of its text boxes to be modified, but this is not what I want.

The reason is that B is already open with some information on it. You press a button, causing A to open, then you select something in A which will cause B to alter the content of some of its text boxes, so I don't want to create a new instance of B: I want to affect the instance of B that is already open.

How do I do this?
Posted
Updated 14-Mar-13 3:15am
v2
Comments
Richard C Bishop 14-Mar-13 8:48am    
It might be a matter of terminology confusion, but you do not call interfaces. They only provide a signature of the methods and do not actually perform any functionality regarding the methods.
Fortan_77 14-Mar-13 8:50am    
Yes, you're correct - my apologies. However I hope the meaning of what I want to do is clear.
Richard MacCutchan 14-Mar-13 9:20am    
If an instance of frmB is already open, then you must have a reference to it somewhere, so you just call the method on that instance variable. Furthermore you do not call the interface, you call the implementation of the method in B.
Fortan_77 14-Mar-13 9:27am    
Yes that's exactly it: how do I call the method on the current instance variable of B?
Menon Santosh 14-Mar-13 9:39am    
i think you want to say that you want to call a method on form, you can call the method by frmB.Method();

Take a static variable in FormB which will hold the reference of currently opened form:

Code in FormB:
C#
public class FormB : .... //interfaces from which you are deriving
{
    public static FormB frmCurrentInstance;

    public FormB(){
        frmCurrentInstance = this;
    }
    .........
    .........
}


In FormA by using the static variable of FormB you can access the current instance of FormB, and using that instance you can invoke the method to perform desired action.

Access the PerformInterfaceMethod() using static variable of FormB.
C#
FormB.frmCurrentInstance.PerformInterfaceMethod();


Hope this solves your requirement.
 
Share this answer
 
Comments
Fortan_77 14-Mar-13 19:11pm    
Perfect. That's exactly what I was looking for. Thanks!
Hi,
There is no reason why you cannot implement the interface on both classes. when you think of interface, you think of the word "can do" or "can have" for example an animal can walk so walk will be an interface "IWalk"; so a given animal for example dog can implement the IWalk but snake will not.

So in code:
C#
public interface IWalk
{
    int NumberOfLegs(int legs);
}

public interface ISwim
{
    int Speed();
}

public class Dog : IWalk, ISwim
{
    public Dog()
    {
    }

    private int NumberOfLegs(int legs)
    {
         // do something with the legs
    }

    private int Speed()
    {
         // how fast dog can swim
    }
} 

public class Cat : IWalk
{
    public Cat()
    {
    }

    private int NumberOfLegs(int legs)
    {
         // do something with the legs
    }
} 

public class Snake : ISwim
{
    public Skane()
    {
    }

    private int Speed()
    {
         // how fast snake can swim
    }
}

public class Animal
{
    private Snake _snake;
    public Animal()
    {
        _snake = new Snake();
    }

    private void GetSnakeSwimSpeed()
    {
        int snakeSwimSpeed = _snake.Speed();
    }
}


Did you see, you can implement an interface to all your required objects.

I hope this helps.

Regards
Jegan
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900