Calling method of a project that is not referenced

-- -- 957 Reputation points
2022-08-29T02:42:58.04+00:00

Hi

I have a Project A which references Project B.

How can I access a method in Project A from Project B? Is there a way to do it using interfaces?

Thanks

Regards

Developer technologies | C#
{count} votes

2 answers

Sort by: Most helpful
  1. Jack J Jun 25,296 Reputation points
    2022-08-29T08:15:26.593+00:00

    @Anonymous , Welcome to Microsoft Q&A, you could try the following steps to call the method in ProjectA from ProjectB.

    First, Please write the code example in One Project:

     public class Student  
        {  
      
            public void SayHello()  
            {  
                Console.WriteLine("Hello, world");  
            }  
        }  
    

    Second, Please right click your References in your visual studio, then click Add reference:

    235704-image.png

    Third, If your projects are in the same solution, you could switch to Projects, then choose the desired project:

    235721-image.png

    If your projects are not in the same solution, you could click Browser.. to find the exe or dll file about another file.

    Then, you could call the method in the current project:

    using ConsoleApp1;  
      
    namespace ConsoleApp2  
    {  
        internal class Program  
        {  
            static void Main(string[] args)  
            {  
                Student student = new Student();  
                student.SayHello();  
            }  
        }  
    }  
    

    Hope the above steps could help you.

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Viorel 122.6K Reputation points
    2022-08-29T18:16:34.8+00:00

    If such cases are not frequent, and you do not want yet to perform the required project changes, you can use delegates.

    For example, if the testB function from Project B must call a function from Project A, you can supply it as an argument:

    // In Project B:  
    namespace ProjectB  
    {  
        public class Class1  
        {  
            public static void testB(Func<int, string> testA)  
            {  
                string s = testA( 123 ); // calling the function from Project A  
                // . . .  
            }  
        }  
    }  
    

    Therefore, testB wants to call a function that takes an integer and returns a string.

    In Project A you will do something like this:

    // the function that will be called from Project B:  
    static string testA( int x )  
    {  
       // . . .  
       return "result";  
    }  
      
    . . .  
      
    // calling testB, passing testA as argument:  
      
    ProjectB.Class1.testB( testA );  
    

    You can also initialize some variables or members of Project B, so that Project B will be able to use them multiple times.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.