Search code examples
c#header-filesvariable-declarationvariable-initialization

C# an organized way to hide (clean up) many declared variables from my main?


So if I have a lot of long global string variables in my main that end up taking up a lot of lines in my main (around 500 to give you an idea) is there a way I can hide them somewhere else but still access them in my main? To better clarify, if this was c++ I'd just make a header with these variables defined and then link the header. Can I do the same with c#?

Here's an example of 1 of many parsed string arrays that contribute 500 lines of code to my main:

string [] m1756EN2T = @"    MODULE Drives (Description := ""~"",
               Parent := ""Local"",
               ParentModPortId := 1,
               CatalogNumber := ""1756-EN2T"",
               Vendor := 1,
               ProductType := 12,
               ProductCode := 166,
               Major := 10,
               Minor := 1,
               PortLabel := ""RxBACKPLANE"",
               Slot := 5,
               NodeAddress := ""192.168.0.1"",
               Mode := 2#0000_0000_0000_0000,
               CompatibleModule := 1,
               KeyMask := 2#0000_0000_0001_1111)
        ExtendedProp := [[[___<public><ConfigID>131178</ConfigID></public>___]]]
        ConfigData := [20,0,393217,33619969,256,0];
        CONNECTION Input2(Rate := 500000,
                           EventID := 0)

        END_CONNECTION

END_MODULE".Split(new string[] { Environment.NewLine }, StringSplitOptions.None);`

EDIT: Please someone with more experience fix the code snippet, not sure what went wrong


Solution

  • If you just want to hide, use #region / #endregion

    If you want something like C++ #include functionality, you can use partial class feature: split your class into multiple source files, some with the strings, another with the actual source code.

    But IMO better solution is move the strings from C# source to some other place. For example, to a string table in a .resx file. This way the IDE will give you nice GUI for editing the data, and the built-in code generator will allow you to refer those strings from C# code by their symbolic names.