none
C# Creating an interface and implementation in shared project RRS feed

  • Question

  • I have a shared C# Project which contains two folders Interfaces and Implementations, it builds but does not auto implement the interface or recognize the interface 

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace InterfacesAndImplementations.Interfaces
    {
        interface ICustomBase
        {
            string Name { get; set; }
        }
    }

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace InterfacesAndImplementations.Implementations
    {
        class CustomBase : ICustomBase
        {
    
        }
    }

    thanks

    Madaxe


    As Busy As A Bricky In Beirut

    Sunday, October 27, 2019 4:31 PM

All replies

  • whats interesting is that if i do this then visual studio seems to knows where the definition is but still wont auto implement the interface 

    namespace SharedProject1
    {
        interface ICustomBase
        {
            string Thingy { get; set; }
        }
    
        class CustomBase : ICustomBase
        {
    
        }
    }


    As Busy As A Bricky In Beirut

    Sunday, October 27, 2019 5:21 PM
  • The problem is that the two files use different namespaces. The important thing for Visual Studio to recognize the interface is the namespace; the fact that it is defined in the same project is irrelevant. Usually, when you add two files to a project, they get by default a namespace that is the name of the project. That is why the code in one file recognizes the definitions in the other file: not because they are in the same project but because they have the same namespace.

    When the two files have a different namespace, the "referencing" class needs the namespace of the "referenced" interface (regardless of whether it is in the same project or a different one). You can either add a "using" statement or write the full name of the interface:

    class CustomBase : InterfacesAndImplementations.Interfaces.ICustomBase

    • Proposed as answer by Ante Meridian Sunday, October 27, 2019 10:42 PM
    Sunday, October 27, 2019 6:31 PM
    Moderator
  • Visual Studio has never auto-implement interface for programmers.

    Instead, you get a little notification on the interface name, where you can right click on it and tell the IDE to auto generate members stubs (contains default signature lines matching the declaration on interface, but throwing NotImplementedException by default) for you.


    Monday, October 28, 2019 2:49 AM
    Answerer
  • Hi Madaxe, 

    Thank you for posting here.

    Based on your description, I make a test on my side.

    First of all, you need to add ‘InterfacesAndImplementations.Interfaces’ reference ( add -> Reference -> Projects ) in namespace ‘InterfacesAndImplementations.Implementations’.

    Then, as Alberto Poblacion suggested, add a "using" statement or write the full name of the interface.

    Here’s the code:

             using InterfacesAndImplementations.Interfaces;
    
             namespace InterfacesAndImplementations.Implementations
             {
                 class CustomBase : ICustomBase
                 {
                     public string Name { get; set; }
                 }
             }

    Hope it can help you.

    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 28, 2019 9:03 AM
    Moderator
  • Hi All,

    I think i missed a really important word in my original post "Shared", I'm working in a shared project, that probably changes things a little bit.

    I normally work in VB and have recently started to learn C#.  Normally i split up my solution into multiple projects entry, implementation, web, infrastructure, UI etc. Doing this in C# does not seem to work.   I can add a link but any dependencies to the link also has to be added.  So i tried shared projects but they seem to have their own mechanisms.

    So, do i create a single project multiple solution folders and files?

    any direction would be helpful, so i changed the interfacesproject to a regular class project and i can create the interfaces and abstract classes fine, but then i want to reference these in another project and i wont let me.

    Thanks

    Madaxe


    As Busy As A Bricky In Beirut





    • Edited by Madaxe Thursday, October 31, 2019 4:27 PM
    Thursday, October 31, 2019 3:45 PM
  • It doesn't matter. You can create one project with multiple folders or you can create more than one project. In both cases the code is the same, as long as there is an adequate Reference between the projects.

    The important thing is that you need to add a "using" that names the namespace of the class or interface that you are calling. Emphasis on "namespace", not "project". The namespace does not necessarily match the name of the project. If you normally work in VB you may expect that the namespace will automatically match the name of the project. In C# you need to open the file that you are calling and see what's written there under "namespace". By default it will be the name of the project followed by a dot and the name of the subfolder. But it may be different if you have been renaming things and/or moving them around.

    Thursday, October 31, 2019 5:02 PM
    Moderator
  • i figured it out, i was missing "public" in front of the interfaces and class by default there is no modifier, now its working thanks

    Madaxe


    As Busy As A Bricky In Beirut

    Thursday, October 31, 2019 8:44 PM
  • Hi Madaxe,

    It seems that your problem has been solved. If so, please click "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 1, 2019 2:24 AM
    Moderator