Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Forms interface
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:
 
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 14-Mar-13 3:36am
Edited 14-Mar-13 4:15am
v2
Comments
richcb at 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 at 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 at 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 at 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 at 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();
Fortan_77 at 14-Mar-13 9:42am
   
This is where I'm getting stuck. If in A, I call the method in B thusly:
 
frmB.PerformInterfaceMethod();
 
I get the error:
 

Error 2 An object reference is required for the non-static field, method, or property 'Project.frmB.PerformInterfaceMethod()'
Jegan Thiyagesan at 14-Mar-13 11:35am
   
You cannot call the method directly like frmB.PerformInterfaceMethod(); unless it is static.
 
from your comments it sounds like you have
 
1) Form B initialised,
2) Form A open
3) you are trying to access a method in from B from Form A.
 
in this case parse the Form B as parameter into form A.
 
public class FormA
{
From _frmB;
 
public FormA(Form frmB)
{
_frmB = frmB;
}
private void DoSomething()
{
_frmB.PerformInterfaceMethod();
}
}
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Take a static variable in FormB which will hold the reference of currently opened form:
 
Code in FormB:
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.
FormB.frmCurrentInstance.PerformInterfaceMethod();
 
Hope this solves your requirement.
  Permalink  
Comments
Fortan_77 at 14-Mar-13 19:11pm
   
Perfect. That's exactly what I was looking for. Thanks!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
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
  Permalink  
v2

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 269
1 OriginalGriff 240
2 CPallini 195
3 Maciej Los 177
4 BillWoodruff 173
0 OriginalGriff 5,655
1 DamithSL 4,506
2 Maciej Los 3,997
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 14 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100