A
A
AleVerDes2013-08-19 22:57:48
.NET
AleVerDes, 2013-08-19 22:57:48

Split project into multiple files?

Hello.
I use SFML.NET and, accordingly, C#. The question arose - is it possible to move certain methods and variables into separate files? For example, I have the gameInit() and gameDraw() methods, as well as the username variable , which is set in Program.cs when the program starts. Can I put these methods in separate files and make the variable global?
As a result, I should be able to call methods in Program.cs with a simple gameInit 'om, and have read and write access to the username variable from any element of the project - be it the main project file or a file with mathematical functions.
Google gives only a certain partial class , which, in the process, cannot be scattered over several files, as well as a categorical answer that there is no creation of global variables and methods in C #.
I ask for help here, for the last stronghold.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Illivion, 2013-08-20
@AleVerDes

is it possible to move some methods and variables into separate files? For example, I have gameInit() and gameDraw(), <...> methods. Can I put these methods in separate files<...>?

Partial class is what you need for your request.
You can create 2 files: Program.Main.cs and Program.Game.cs (names can be any) and place a partial class inside:
Program.Main:
static partial class Program
{
    static void Main() 
    {
        GameInit();
        GameDraw();
    }
}

Program.Game:
static partial class Program
{
    static void GameInit()
    {
        // Game init here
    }
    static void GameDraw()
    {
        // Game draw here
    }
}

and make the variable global

The most global! :)
static partial class Program
{
    public static String Username;
}

You can access it from anywhere like:
var username = Program.Username;

D
Dmitry Guketlev, 2013-08-20
@Yavanosta

There are no global variables and methods in c#. You can make a static (or normal) class and place a static field (property) and static methods in it. Get about what you need.
The partial only allows you to place the description of the class in several files for convenience.
In general, in c # all classes, nothing can be created outside the class (structure). Those. there are no global variables and methods. But a static method and a static field (property) are about what you need.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question