Home > other >  How can I split my code (c#) in Visual Studio while maintaining full functionality?
How can I split my code (c#) in Visual Studio while maintaining full functionality?

Time:04-25

I realize this sounds like a question the answer to which can be found in the first google link, but to my surprise it wasn't. I'm learning C# recently and for the first time I'm writing a fairly large project, which at the moment contains more than 200 lines of code and, according to my estimates, should contain more than 1000 in the end.

I understand that this is not a problem for experienced programmers, but I'm starting to get confused.I have found some answers on pulling classes from neighboring files, but my code consists almost entirely of methods and I have not been able to interpret these answers to my advantage. Again, this is most likely due to my inexperience.

i want my files to look something like this:

program1.cs

int x = 25;

program2.cs

Console.Write(x);

As you can see, this does not happen. I have tried adding the CS file either manually or through the solution explorer. Nothing helps, so I really hope to get an answer here. How can I get all methods and variables from one file to work in another in VS? Additional question: If there is no such possibility at all, can I somehow visually hide a piece of my code from myself, just so that it does not bother me until I need to change something in it?

P.S. Yes, I understand that if it is easy to get confused in the code, then the code is poorly composed. I'm also working in this direction, but I would still like to know the answer.

CodePudding user response:

If your class is so big you want to split it into multiple files it’s likely that you should also be splitting it into multiple classes that each perform a simpler job. To access public methods and variables of one class from another class, either they need to be static (meaning there’s only ever one of that thing basically) in which case you can activate them using the name of the class, e.g.:

Class1.cs

public static int x = 25;

Class2.cs

Console.Write( Class1.x );

Or you need a reference to a specific instance of that class, e.g.

Class1.cs

public int x = 25;

Class2.cs

Class1 instance = new Class1();
Console.Write( instance.x );

CodePudding user response:

Ok, so here is the code I promised.

Here are the two files I created

Program.cs

using System;

namespace splitClass
{
    // You can either write a class here and use it on the other file
    public class HelloThere
    {
        public HelloThere()
        {
            Console.WriteLine("Hello, there");
        }

        public static int add(int a, int b)
        {
            return a   b;
        }

        public int subtractFromTen(int c)
        {
            return 10 - c;
        }

        static void Main(string[] args)
        {
            // You can also use the class in the other file
            UseHelloThere useHelloThere = new UseHelloThere();

            Console.WriteLine(useHelloThere.add(8, 9));
        }
    }

    // or split a class into two using partial keyword
    public partial class SomeOtherClass
    {
        public int abc;
        public SomeOtherClass()
        {
            abc = 87;
        }

        public int getAbc()
        {
            return abc;
        }

        public int add(int b)
        {
            return b   abc;
        }
    }
}

Other file.cs

using System;

namespace splitClass
{
    // since namespaces are same, you can use the other class from the same file
    public class UseHelloThere
    {
        HelloThere hello;
        public UseHelloThere()
        {
            hello = new HelloThere();
        }

        public int add(int a, int b)
        {
            return HelloThere.add(a, b);
        }
    }

    // or you can write the continuation of the other class
    public partial class SomeOtherClass
    {
        public int subtract(int a)
        {
            return abc - a;
        }
    }
}

CodePudding user response:

If you want to get all methods and variables from one file to work in another in VS, you can refer to the following steps:

First, define variables and methods in Program1.cs such as:

namespace ConsoleApp7
{
    namespace ConsoleApp
    {
        public static class Program1
        {
            static void Main(string[] args)
            {
            }
            public static int x = 25;
            public static void add()
            {
                x  ;
            }
        }
    }
}

Second, add project reference in Program2.cs. enter image description here enter image description here

Finally add using in Program2.cs and then you can use the variables and methods defined in Program1.cs.

using ConsoleApp7.ConsoleApp;
class Program2
{
    static void Main(string[] args)
    {
        Console.WriteLine(Program1.x);
        Program1.add();
        Console.WriteLine(Program1.x);
    }
}
  • Related