I think it's simple .. and if I'm doing it wrong, please just let me know .. I'm not so far in I can't fix it.
I wanted a separate .cs file to hold my variables that my entire project would use. I created the class like this to house the variables:
This is very dumbed-down obviously.Code:namespace MyNamespace { public static class VariableDeclarations { public class Variables { public int[, ,] Playfield = new int[50, 50, 50]; } } }
To access this class from the main program .cs file .. I had to use
..outside of the classes so it could be accessed by all the classes in that .cs file.Code:VariableDeclarations.Variables Vars = new VariableDeclarations.Variables();
This worked great and I could use Vars.Playfield[x,y,z] without an issue, but when then I needed to create a method to initialize this playfield with 1.
The method is set up like this and is also in it's own separate.cs file.
Everything is in the same namespace.Code:public class Initialize { //VariableDeclarations.Variables Vars = new VariableDeclarations.Variables(); public static void InitializePlayfield() { VariableDeclarations.Variables Vars = new VariableDeclarations.Variables(); Vars.ZLoop1 = 0; while (Vars.ZLoop1 < 12) { Vars.YLoop1 = 0; while (Vars.YLoop1 < 12) { Vars.XLoop1 = 0; while (Vars.XLoop1 < 12) { Vars.Playfield [Vars.XLoop1, Vars.YLoop1, Vars.ZLoop1] = 1; Vars.XLoop1 = Vars.XLoop1 + 1; } Vars.YLoop1 = Vars.YLoop1 + 1; } Vars.ZLoop1 = Vars.ZLoop1 + 1; } } }
Now when I call this method, it doesn't save the values in the Playfield variable. When it comes to the point of accessing it in the main loop, the values are gone.
I'm wondering why the values seem to be reset back to 0 or null after I go from one class to another. I'm very new to C#, so don't laugh.
I was guessing it was because I had to use 2 separate:
..commands in 2 separate .cs files and there were 2 instances of Vars. But then again, I thought Vars would be public and accessible across all of the .cs files.Code:VariableDeclarations.Variables Vars = new VariableDeclarations.Variables();
Also, please correct my terminology. I'm going all out and want to start this right and get in the good habits.
Thanks in advance!
Sinc'
Sketch



LinkBack URL
About LinkBacks



