none
Visual Studio 2019/C#: How do I instantiate a class in a separate class file in Program.cs Main method? RRS feed

  • Question

  • I am learning the program.  I created a regular solution for a blank Console app & am using C#.  As my Program.cs is getting a bit cluttered, I decided to create a separate file using Ctrl + N, & chose Visual C# Class.  

    Since my code is just a bunch of random things, it might be easier to just tell you the spot with the error.  Within namespace TestingA, in class Program, & in the Main Method I have: Linker link = new Linker();

    In separate class file I have also used namespace TestingA, public class Linker with a single field in it.

    I have tried putting a using directive at the top of the class file that references TestingA.  I have tried adding a reference to the project.  I have tried calling the class explicitly, but nothing seems to work.

    Any ideas?  

    Tuesday, June 25, 2019 5:58 AM

All replies

  • It looks good, the way that you describe it.

    If both files contain code written in the same namespace (TestingA) they should be able to call each other without needing any additional "using" directive.

    So Linker link = new Linker() should work perfectly well if Linker is a public or internal class in the second file.

    If it doesn't work, there must be something else that is not obvious from the text of your question. It would be a good idea to provide us with the complete and exact error message, rather that just saying that "it doesn't seems to work". It will also be helpful to see the relevant pieces of code (paste them into a message in the forum using the "Insert Code Block" icon). We may be able to see something that is not obvious from the general description that you provided.

    Tuesday, June 25, 2019 6:19 AM
    Moderator
  • You need to show the code on what you are trying to do. You format tool on the toolbar next to the HTML icon to format the code in your post before you post the code.
    Tuesday, June 25, 2019 8:44 AM