0

I'm working on some C# code where I have several files that use the same public variables (let's call these variables a, b, and c for now). Disclaimer: please don't berate me on the quality of the code, I didn't write it, it's simply my job to fix it. These variables are public and shared across all files, and should really be an array (a, b, and c all do the same thing, with slightly different properties). I am modifying the largest file of the bunch to take out all references to the variables and replace them with an array letters[], but other files contain disparate references to these variables. Is there any way to, in other files, define a variable or macro of some kind so that I don't have to change every reference in every file?

For example: In the biggest file "main.cs", I used to have:

    public string a;  
    public string b;  
    public string c;  
    a = "a";  
    b = "b";  
    c = "c";  

but I fixed it to have:

    string[] letters;  
    letters[0] = "a";  
    //etc

Now, in file "small.cs", I have

    a = "hello world";  
    b = "goodbye world";  

Instead of having to go through every single file, is there any way that I could just have 'a' be defined as the first element of letters (letters[0]), b reference letters[1], etc? This way, the program would still run, and in the small files, C# would know that any reference to 'a' really means a references to letters[0].

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

3 Answers3

6

Use a property:

public string A { get { return letters[0]; } }
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
1

Reference them from a property:

public string a
{
     get
     {
           return letters[0];
     }
     set
     {
           letters[0] = value;
     }
}
Dan Linus
  • 94
  • 3
0

instead of writing

     public string a;
     public string b; 
     public string c;
     a = "a";
     b = "b";
     c = "c";   

write

     private List<string> vals = new List<string>{ "a", "b", "c" };
     public string a { get { return vals[0]; } set { vals[0] = value; } }
     public string b { get { return vals[1]; } set { vals[1 = value; } }
     public string c { get { return vals[2]; } set { vals[2] = value; } }
Charles Bretana
  • 143,358
  • 22
  • 150
  • 216