0

I have a interface called iChannels, which has only one method.

using CHNL;
namespace iInterface
{
    public interface iChannels
    {
        string getData(string docXML);
    }
}

Then in another project I have a class called Channel1, wich is defined like this:

using iInterface;
namespace CHNL
{
   public class Channel1:iChannels
   {
       string getData(string str)
       {
           return str;
       }
   }
}

I had to make a cross-reference, for the interface and class know each other. After that I have a webform, and I just want to use using iInterface;, but if I only do that, I can't create Channel1 objects.

My intent is to create Channel1 objects, just using iInterface Library.

  • 2
    I do not understand what do you mean, but are you sure that this `Channel1:iInterface` shouldn't be `Channel1:iChannels`? Also1: YOU CANNOT add to interface a reference to its implementation class. Also2: Please define `I can't`. Nothing in your code shows us the problem you're describing - no cross-referencing there. – Tarec Feb 25 '14 at 11:40
  • And please adhere to the Microsoft Coding Conventions found here: http://msdn.microsoft.com/en-us/library/ff926074.aspx – Zache Feb 25 '14 at 12:07
  • CHNL project is using iInterface, and iInterface is using CHNL. This is not cross reference? – Emmanuel Tavares Feb 25 '14 at 12:59

2 Answers2

0

You cannot inherit from iInterface as iInterface is the name of a namespace and not the interface, so I think you need the following code;

using iInterface;
namespace CHNL
{
   public class Channel1 : iChannels
   {
       string getData(string str)
       {
           return str;
       }
   }
}

Your webform does not need to know about the Channel1 class but the project your webform is in will need to reference the project Channel1 is in.

You can create an instance of iChannels using something like;

 iChannels myIChannels = new Channel1();

Your webform can then reference iChannels without being aware of the actual concrete implementation of iChannels, in this case Channel1.

It is better practice though to use either a factory or dependency injection to create actual implementations of iChannels and avoid using 'new Channel1();' altogether.

daniellepelley
  • 1,939
  • 1
  • 11
  • 12
  • 1st - It was a mistake, because i did the "public class Channel1 : iChannels". Sorry for that. 2nd - "iChannels myIChannels = new Channel1();" - that's exactly my problem, because "new Channel1();" is not recognized. – Emmanuel Tavares Feb 25 '14 at 12:10
  • I don't know anything about factory methods or dependency injection. I watched some tutorials but i don't understand. Do you know some good tutorials? Please. – Emmanuel Tavares Feb 25 '14 at 13:02
  • It depends what sort of application you are building Asp Webforms http://msdn.microsoft.com/en-us/library/ff664622(v=pandp.50).aspx Mvc http://www.asp.net/mvc/tutorials/hands-on-labs/aspnet-mvc-4-dependency-injection There are plenty of tutorials out there. Firstly though I would try to choose a DI container. Unity and Ninject are 2 commonly used ones. – daniellepelley Feb 25 '14 at 13:31
0

I think I can see your issue now.

When you say 'My intent is to create Channel1 objects, just using iInterface Library'...this is not possible in C#, or at least not possible directly creating Channel1 objects without Channel1 being declared in the same library as iChannels.

You can achieve this though through dependency injection. Broadly speaking your webform will be dependent on iChannels and a dependency container will provide it will an implementation of iChannels, in your case Channel1.

Have a look at this article;

Why does one use dependency injection?

Community
  • 1
  • 1
daniellepelley
  • 1,939
  • 1
  • 11
  • 12
  • I understood the factory method. I aplied to my project. I have one client aplication, a interface, a factory and a class library where i keep all my classes. I've included a reference to my factory on the client project, and then all my classes have a reference to my factory too. It worked. Thanks. – Emmanuel Tavares Feb 25 '14 at 16:42