views:

190

answers:

7

hi.

i have two classes which have some common methods like funcA(), funcB()

and some methods are only related to its class...

what i did is made interface of TestInterface

public interface TestInterface
{
   void funcA()
   void funcB()
}


public class ClassA : TestInterface
{
  public  void funcA()
   {
     Console.WriteLine("This is ClassA FuncA()");
   }
  public  void funcB()
   {
     Console.WriteLine("This is ClassA FuncB()");
   }
  public void myFuncA()
   {
      Console.WriteLine("This is My Own Function A");
    }

}

public class ClassB : TestInterface
    {
      public  void funcA()
       {
         Console.WriteLine("This is ClassB FuncA()");
       }
      public  void funcB()
       {
         Console.WriteLine("This is ClassB FuncB()");
       }
      public void myFuncB()
       {
          Console.WriteLine("This is My Own Function B");
       }

    }



public static void main()
{
 TestInterface test = new ClassA();
 test.funcA();
}

as u see in above two classes. i have two functions myFuncA() and myFuncB() are not part of interface. they only belongs to their own class.

how can i call them from the main method. where i am creating object of TestInterface and initializing it with some child class.???

+1  A: 

If you're asking whether you can do something like:

public static void Main(string[] a)
{
 TestInterface test = new ClassA();
 test.myFuncA();
}

the answer is no. You would have to cast it to ClassA first. The fact that you think you need to do this indicates there is probably something wrong with your design.

Matthew Flaschen
A: 

Make a new interface for ClassA and ClassB and you just need to cast the class to the correct interface to see the new functions.

public class ClassA : TestInterface, TestInterfaceA {
}

So, when you get the concrete instance just cast that to TestInterfaceA to see the functions specific to this class.

James Black
A: 

You can cast the TestInterface object, but it defeats the purpose of polymorphism...

public static void main()
{
 TestInterface test = new ClassA();
 test.funcA();
((ClassA) test).myFuncA();
}
Mr. Will
A: 

Try casting. I assume from the question you meant something like this:

public void simpleMethod(TestInterface variableName) {
   if (variableName is ClassA) {
       ((ClassA)variableName).myFuncA();
    }
}
Cuga
A: 
is it good to add methods of ClassA and ClassB into interface and provide null implementation in other class?
like 

public interface TestInterface
{
void funcA();
void funcB();
void myFuncA();
void myFuncB();
}


public class ClassA:TestInterface
{
public void funcA()
{
 // some code here
}

public void funcB()
{
 // some code here
}
public void myFuncA()
{
 // my own code here
}
public void funcB()
{
 // null implementation
}
}

and vise versa of this in ClassB

Mohsan
That's the Null Object pattern. But you should seriously consider whether this is necessary either.
Matthew Flaschen
A: 

actually i have separate classes in DAL.

they have some common methods. and some methods related to their own class.

i want to implement Factory Pattern on this.

what should i do now?

Mohsan
Galilyou
A: 

Mohsen I'm really confused with your question

if you just want to call MyFuncA() or MyFuncB() just you should create an instance of their own calss like

ClassA a = new ClassA();
a.FuncA(); //interface member
a.FuncB(); // interface member
a.MyFuncA(); //class A Member

I can't undrestand why you are trying to create an instance of your Interface and then call subclass methods!!!! Am I right ? or Imisunderstood your issue ?

Nasser Hadjloo