Can I access variables from a different class without changing the default modifier?

Here’s some example code. It won’t run because “OBJ1.myArray is inaccessible due to its protection level”. I know if I want the code below to run I can just add “public” before my int[] declaration or add getters and setters. However, I don’t want to do that.

I would like to know if it is possible to allow my class variables to be exposed to each other in a similar way that Java sets everything to “public” by default.

namespace src { public class OBJ1 { int[] myArray; //this line public Room(){ myArray = new int[10]; } } } 

namespace src { public class Program { public static void Main(string[] args) { OBJ1 test = new OBJ1(); test.myArray[0]=5; //And this line } } } 

submitted by /u/Crippled_shadow
[link] [comments]

Leave a Reply