There is a solution in which two projects are created. In one of them, a class with methods was created. This project is connected to another by reference.

How do I pass class methods from the first project to the second?

This is the structure of the solution:

enter image description here

  • make them public - Grundy
  • The bottom line is that the class is not visible in the project to which the project with the class is connected, so I can’t access methods because I cannot create an object. - Nicholas
  • @Nicholas It's time to show the code. Even I am somehow ashamed to ask - namespace write the namespace in using ? Target framework for both projects the same? - Igor
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

2 answers 2

If you want to use methods FROM any class from another assembly, then you need to have these methods public in this class (you can make them protected and create a successor). The class must also be public (or protected and make a successor). In general, you can do everything internal and add InternalsVisibleTo to the assembly where you want to use the internal methods, all at your discretion.

Sample code where Assembly 1 is connected to Assembly 2

 // Assembly 1 public class Class1 { public void Method1() { } } // Assembly 2 new Class1().Method1(); 

Update

The class must be in a specific namespace, try to find a line in the ArrayClass.cs file that contains the word namespace . On the same line, after the word namespace the name of your space is specified, try inserting it directly before the class, for example, if in my example Class1 has Assembly1 , then from the second assembly you can refer to it like this: Assembly1.Class1 (and create it like this: new Assembly1.Class1() )

Update 2

I noticed a little strange thing, maybe this is a coincidence, but still, I dare to assume that your first and second projects are WinForms applications. Although, for your question, you should expect that WindowsFormsApplication1 will be like a class library (.dll), but still, .exe files can also be added to links to other projects. Make sure that the link is exactly present, because adding another project to the solution is not enough. Just in case: https://msdn.microsoft.com/ru-ru/library/wkze6zky(v=vs.120).aspx

  • img-host.org.ua/images/mmmmmm.png This is the structure of the solution, I need to transfer the methods from the ArrayClass class to the second project, all I can pass on is the methods from the Form1 class - Nicholas
  • @Nicholas Can you show the contents of ArrayClass.cs (or is it secret?) And how do you use it? It is recommended to do this by editing the question. - Igor
  • There usual methods of sorting (merge, a bubble, an insert). And I use them to compare the efficiency of each method, the first project compares the sorting by insertion and bubble, and in the second project, it is necessary to implement comparisons of sorting by insertion and merge. - Nicholas
  • @Nicholas And why are you telling us all this? You need to do so that the code namespace A { public class B { public int GetValue() { return 123; } } } namespace A { public class B { public int GetValue() { return 123; } } } and int a = new AB().GetValue(); earned. - Igor

No, you can bring this class to the third project and transfer it to the first and second dependencies.

But you cannot access the code in the dependent class.

Update

Everything is simple, as I understood from the author Project1 refers to Project2, thus Project2 classes are available in Project1, but not vice versa. In Project2, there will be no access to the classes of Project1 and it cannot refer to Project1 since there will be a cyclic dependency. In general, two projects cannot refer to each other, only one to the other. If you want some class from Project1 (provided that it refers to Project2) to be available in Project2, then this will not work, but you can make Project3 to which Project1 and Project2 will refer and put into it the class that should be shared.