0

I am not a professional programmer and trying to develop a desktop WinForms app using Visual Studio for my specific purpose. I made some search and after hours couldn't find an answer to my issue. Basically, I have hundreds of variables and methods in my WinForms program's Form1 class. All the code is in this Form1.cs page. But I came to a point I have to scroll crazy each time I try to reach a variable or a method. C# books and sites I looked at do not show such practical aspect.

For instance my Forms1.cs starts like:

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {     
      int var_1 = 1;
     //...

So as you see above, I have hundreds of variables such as var_1 inside Form1.cs. Now I want to move them to separate file so I created a class new called named Vars to the project and made the var_1 access modifier "public static" such as below:

namespace WindowsFormsApplication2
{
    class Vars 
    {
      public static int var_1 = 1;
      //... 

Now I deleted var_1 from Form1.cs and I can access var_1 in Form_cs typing as Var.var_1. But now I have thousands of variables I need to add "Var." before all of them in Form1.cs. Is there any way that I can directly access variables in this case without using "Var." but only using var_1 as in the first original case?

floppy380
  • 179
  • 1
  • 8
  • The short answer is, no. The reason you need to use Var.var_1 is because the location of the variable is in Var. So, you must tell the program to go to Var to find var_1. This would be the same if you use an instance class or static class. However, it sounds like there is a fundamental issue if you're having to use "thousands" of variables in a single form. Perhaps look into [Arrays](https://docs.microsoft.com/en-us/dotnet/api/system.array?view=netframework-4.8), [Lists](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=net-5.0), Tuples, other collection types. – bPuhnk Feb 04 '21 at 21:36
  • @bPuhnk My problem is I did the coding(it took me 6 months) for a very specific application in "procedural way" using no classes at all just methods event handlers and variables all in one page. I didnt need OOP for my purpose. But now all my code is in single page I have to use I guess making classes and putting methods and variables inside these classes as separate files. I am trying to make the Form.cs as short as possible. Scrolling became painful. Maybe my bad but I really couldnt find a tutorial about this structuring the WinForms application. – floppy380 Feb 04 '21 at 21:41
  • I reopen as the [suggested duplicate](https://stackoverflow.com/questions/15674752/using-variables-and-methods-from-separate-cs-files-in-c-sharp) doesn't have an accepted answer. – Reza Aghaei Feb 04 '21 at 22:34
  • @RezaAghaei: having an accepted answer is not a reason to reopen a duplicate. This question is a duplicate and you are abusing your privileges (and in a self-serving way to boot) by reopening a question that should not have been reopened. Especially considering there are at least _two_ valid duplicate targets. – Peter Duniho Feb 04 '21 at 22:35
  • Does this answer your question? [How to use separate .cs files in C#?](https://stackoverflow.com/questions/572314/how-to-use-separate-cs-files-in-c) – Peter Duniho Feb 04 '21 at 22:37
  • Does this answer your question? [using variables and methods from separate .cs files in c#](https://stackoverflow.com/questions/15674752/using-variables-and-methods-from-separate-cs-files-in-c-sharp) – Peter Duniho Feb 04 '21 at 22:38

1 Answers1

3

In general you should not have such a class with hundreds of variables and methods. It's showing a design problem.

But considering the fact that you told 'I am not a professional programmer' I assume you are looking for a practical advice which may make things easier for you:

You can move those variables and methods to another file but keep them in the same class, by using C# partial class. To do so, just add the partial keyword you your class definition, create the other part in another file, with the same name and same namespace, with partial keyword, then cut the variables from first part and paste in the second one.

public partial class Form1
{
    //methods
}

public partial class Form1
{
    //variables
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • But will there be two files with the same name as Form1.cs inside the solution? – floppy380 Feb 04 '21 at 21:56
  • File name is not important, but the class name is. Just rename the second to something meaningful for yoursef like `Form1.Variables.cs` – Reza Aghaei Feb 04 '21 at 21:56
  • And this is the answer to your next question, before you ask [Prevent Visual Studio load a custom partial class as a designer](https://stackoverflow.com/a/40302216/3110834) – Reza Aghaei Feb 04 '21 at 22:01