locked
Calling interface driven method from one project to another project by calling through Interface refrences (Check the description below in details) RRS feed

  • Question

  • User-560849474 posted

    namespace Project1
    {
    public interface IInterface1
    {
    void Method();
    }

    public interface IInterface2 : IInterface1
    {
    void Method();
    void Method2();
    }

    // Here we are implementing methods for get and sent data to database

    public class GetSentData: IInterface1
    (
    void Method();
    void Method2();
    )
    }

    note: We are making a .dll of above mentions interfaces and class.

    //This is a another solution
    //I add reference of Project1 into project2

    namespace project2
    {
    public class Acess
    {
    //Here we want to call above class method by using the interface driven class
    IInterface1 myinterface=new GetSentData();

    myinterface.Method();

    //Like This but am getting error as: Object is not set to an instance of object
    }
    }

    //Please Suggest with your valuable answers

    Monday, August 21, 2017 10:30 AM

All replies

  • User-1509636757 posted

    There are code issues. Please check out below corrections:

    namespace Project1
    {
        public interface IInterface1
        {
            void Method();
        }
    
        public interface IInterface2 : IInterface1
        {
            new void Method();
    
            void Method2();
        }
    
        public class GetSentData : IInterface1
        {
            public void Method()
            {
            }
        }
    }
    namespace project2
    {
        public class Acess
        {
            public void SomeMethod()
            {
                IInterface1 myinterface = new GetSentData();
                myinterface.Method();
            }
        }
    }
    {
        public partial class WebForm271 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                Acess access = new Acess();
                access.SomeMethod();
            }
        }
    }

    Also, keep a practice of using class constructors.


    Tuesday, August 22, 2017 8:51 AM
  • User-560849474 posted

    Thank you for replying.

    You are absolutely correct. 

    I am not able to call GetSentData() by creating object of IInterface1 from another project of another solution.

    Because we can't create the reference of Interface from anywhere.

    If I am doing that same thing, I am getting exception of Object is not set to an instance of object.

    Tuesday, August 22, 2017 11:16 AM
  • User303363814 posted

    Please use copy and paste to show your code.  Also use the 'Show code' button on the editor toolbar.

    Please use copy and paste to show the exact error.  Please specify when you get the error, runtime or compile time.

    When you say 'there is a another solution' do you mean a Visual Studio solution or a separate way to solve your problem?

    Try to make as simple an example of your problem as possible.  For example, what does IInterface2 have to do with anything? 

    I suspect there is code between the statement which initializes myinterface and the statement which calls Method.  Please show all the code from the smallest example which reproduces your problem.

    BTW - Attempting to make a minimal program which reproduces your problem will quite often enable you to see what the problem is in the original code

    Thursday, August 24, 2017 2:16 AM