1

I had a problem with the following declaration:

byte[] frameBuffer = new byte[VideoFile.FRAME_SIZE];

I had this declared as a variable in a class I created. Unfortunately, Visual Studio placed a squiggly yellow line under it and declared "FRAME_SIZE" as not existing in the current context. The squiggly yellow line is what bothers me. If I give it a completely non-existent name it uses a squiggly red line, but since FRAME_SIZE does it exist I get a yellow line.

FRAME_SIZE is declared in the VideoFile class as follows:

public static readonly int FRAME_SIZE = 2621440;

It works in other cases when I reference it, but just not in this particular case. I have tried experimenting with other declarations that do not give me the dreaded yellow squiggly:

First experiment:

    const int NEW_FRAME_SIZE = 256;
    byte[] frameBuffer2 = new byte[NEW_FRAME_SIZE];        

Second experiment:

    int thisworks = VideoFile.FRAME_SIZE;

I finally placed my original declaration within the class' constructor and it worked:

byte[] frameBuffer = new byte[VideoFile.FRAME_SIZE]; // placed in constructor

Researching this issue on yielded many results, but none which particularly answer my question:

The name 'controlname' does not exist in the current context

The name 'controlname' does not exist in the current context

The name XXXX does not exist in the current context

I think the issue relates to not being able to use this particular variable for initialization outside of the constructor, but I would like to get a definite answer along with a reference to where I can research this further.

Community
  • 1
  • 1
gonzobrains
  • 7,856
  • 14
  • 81
  • 132
  • 1
    `I had this declared as a variable in a class I created` : could you give a little bit more context ? Where, how did you declared this variable ? – Raphaël Althaus Sep 05 '13 at 19:26
  • Your question does not give us enough information to reproduce the issue you have. Can you provide more code such that the code you post in the question, exhibits the problem (a minimal example)? – Jeppe Stig Nielsen Sep 05 '13 at 19:45
  • I declared the variable at the top of the class outside of any method/constructor. – gonzobrains Sep 05 '13 at 19:51
  • Are you sure it wasn't _The name `VideoFile` does not exist in the current context_? I understand `frameBuffer` was an instance field of a class. Was `frameBuffer` declared inside the `VideoFile` class? It is really hard to help you when you don't give us code that illustrates the problem. I think we need a [Short, Self Contained, Correct (Compilable), Example](http://sscce.org/) here. – Jeppe Stig Nielsen Sep 05 '13 at 20:56
  • I'm not sure why VideoFile would not exist in the current context, as I am referring to it on many other lines within the same file. That class is part of the project I am working on and not included via a using statement. The problem just seems to be the way I am initalizing framebuffer by using a constant included in the VideoFile class. I'm not exactly sure though. – gonzobrains Sep 08 '13 at 20:04

1 Answers1

2

You're seeing this behavior because FRAME_SIZE is static and can only be accessed from a static context. The constructor can access FRAME_SIZE because a constructor is executed before a static member variable reference is created.

Other classes declared as static will be able to access FRAME_SIZE (as long as their access modifiers permit).

Related Question

MSDN Article

Community
  • 1
  • 1
JeremiahDotNet
  • 910
  • 4
  • 9