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].

1
  • have you tried getters and setters ? Commented Aug 15, 2012 at 19:55

3 Answers 3

6

Use a property:

public string A { get { return letters[0]; } }
Sign up to request clarification or add additional context in comments.

Comments

1

Reference them from a property:

public string a
{
     get
     {
           return letters[0];
     }
     set
     {
           letters[0] = value;
     }
}

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.