-2

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.

diZb
  • 27
  • 6
  • put them in the same ```namespace``` maybe? I may post some code later – Özgür Güzeldereli Apr 24 '22 at 22:02
  • 2
    Are you searching for this? [Partial Classes and Methods](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/partial-classes-and-methods) – Theodor Zoulias Apr 24 '22 at 22:11
  • 2
    "can I somehow visually hide a piece of my code from myself" https://stackoverflow.com/questions/44390454/what-is-the-use-of-region-and-endregion-in-c – gunr2171 Apr 24 '22 at 22:31
  • Are you using `class`es? Or is this all one big "script"? – Lance U. Matthews Apr 24 '22 at 22:34
  • @ÖzgürGüzeldereli Yes, I would be very grateful if you do – diZb Apr 24 '22 at 23:16
  • @LanceU.Matthews it's a big script. A text generator from many variables, to be precise. – diZb Apr 24 '22 at 23:18
  • 1
    The code _generates_ text or is, itself, _generated_ text? Are you using (multiple) methods (functions), at least? Code in C#/.NET/OOP is defined within `class`es (see also `struct`s), although not explicitly if you're using [top-level statements](https://docs.microsoft.com/dotnet/csharp/fundamentals/program-structure/top-level-statements). A `class` can be split across multiple `.cs` files but a method cannot, so you'll need at least one method per file. I think you'll fare better if you learn/follow the prescribed syntax and conventions of C#, rather than trying to get it to follow you. – Lance U. Matthews Apr 25 '22 at 04:11

3 Answers3

1

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 );
Sven Viking
  • 2,660
  • 21
  • 34
0

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;
        }
    }
}

0

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);
    }
}
Jingmiao Xu-MSFT
  • 2,076
  • 1
  • 3
  • 10
  • This seems to suggest that each code file must be compiled to its own assembly for this to work, when, in fact, all of your `public` modifiers could be changed to `internal` and this sharing of members across `class`es could take place within one assembly. – Lance U. Matthews Apr 25 '22 at 18:54