3

I'm new to C# - this is nearly my first program. I'm trying to create some public static variables and constants to use anywhere in the program. The - wrong - way I have tried is to declare them in a separate class in the same namespace but they are out of context for the main program. It's a WPF application. The code looks like 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;    // this compiles
            if (jjj < BuffOneLength)    // "the name 'BuffOneLength' does not exist in the current context"
            {
                mmm = Buff2[0]; // "the name 'Buff2' does not exist in the current context"
            }
        }
    }
}

My actual program is much longer of course. I created the above WPF application exactly as shown to test this problem and I got these errors, also occurring in the real program. I really don't want to fill the arrays in the main program as they are very long and it would mean much scrolling. I also want to have one place where I can declare certain public static variables. What is the right way to do this?

Cee McSharpface
  • 8,493
  • 3
  • 36
  • 77
Moonling
  • 59
  • 2
  • 9
  • I don't get why are you declaring the same namespace twice? – dcg Apr 14 '17 at 13:35
  • The 2 blocks are in 2 different .cs files in the project. Visual Studio generates the namespace declarations. I appended them in the example above. – Moonling Apr 14 '17 at 13:38
  • Ok! Didn't read it any where so assumed that was in the same file. – dcg Apr 14 '17 at 13:40

2 Answers2

8

You have to either specify class:

// BuffOneLength from PublicVars class
if (jjj < PublicVars.BuffOneLength)  {
  ...
  // Buff2 from PublicVars class 
  mmm = PublicVars.Buff2[0];

or put using static:

// When class is not specified, try PublicVars class
using static testXyz.PublicVars;

namespace testXyz {
  public partial class MainWindow : Window {
    ...

    // BuffOneLength - class is not specified, PublicVars will be tried  
    if (jjj < BuffOneLength) {
      mmm = Buff2[0]; 
Community
  • 1
  • 1
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
2

You can't access a static variable that is in another class by just calling the variable. You need to first go thru the class that contains it in your case it would be

PublicVars.BuffOneLength

and

PublicVars.Buff2[0]
I.B
  • 2,925
  • 1
  • 9
  • 22
  • Thanks. That answers it. I will prefix the vars with the class name. "Using static" would be a nice option, but I only have VS 2013... – Moonling Apr 14 '17 at 14:06