wpf - Not scoping static vars properly in C# -
i'm new c# - first program. i'm trying create public static variables , constants use anywhere in program. - wrong - way have tried declare them in separate class in same namespace out of context main program. it's wpf application. code looks this:
namespace testxyz { class publicvars { public const int buffonelength = 10000; public static int[] buff1 = new int[buffonelength]; public const int bufftwolength = 2500; public static int[] buff2 = new int[bufftwolength]; private void fillbuff1() { buff1[0] = 8; buff1[1] = 3; //etc } private void fillbuff2() { buff2[0] = 5; buff2[1] = 7; //etc } } }
second file:
namespace testxyz { public partial class mainwindow : window { public mainwindow() { initializecomponent(); } public static int isincontext = 0; int jjj = 0, mmm = 0; private void dosomething() { isincontext = 5; // compiles if (jjj < buffonelength) // "the name 'buffonelength' not exist in current context" { mmm = buff2[0]; // "the name 'buff2' not exist in current context" } } } }
my actual program longer of course. created above wpf application shown test problem , got these errors, occurring in real program. don't want fill arrays in main program long , mean scrolling. want have 1 place can declare public static variables. right way this?
you have either specify class:
// buffonelength publicvars class if (jjj < publicvars.buffonelength) { ... // buff2 publicvars class mmm = publicvars.buff2[0];
or put using static:
// when class not specified, try publicvars class using static testxyz.publicvars; namespace testxyz { public partial class mainwindow : window { ... // buffonelength - class not specified, publicvars tried if (jjj < buffonelength) { mmm = buff2[0];
Comments
Post a Comment