The Name Does Not Exist In the Current Context, Even Though I Just Declared It

I’m having a problem where no matter what kind of object I type in code, Visual Studio won’t recognize the reference on a later line. For instance, right now I have

int[] listofsetups;
listofsetups = new int[4];

and when I put my mouse over the second listofsetups, it tells me “The name ‘listofsetups’ does not exist in the current context.” If I declare and create the array in one line, trying to access listofsetups on a later line will give me the same error message. The editor is doing this for every kind of object I try to create, so I’m suspecting it’s something to do with the editor although I restarted and updated it and that didn’t fix anything. Any help solving this problem would be much appreciated.

Can you share more of the code here? Is this inside a method? Directly inside of a class? If it’s directly in a class you have to declare and initialize in the same line, and generally can’t reference other variables. You need to put any code that references the variables into methods.

It was directly inside of the class, not in a method. I moved the second line to a method and that fixed it. Dumb newbie mistake on my part. Thanks so much for your help!

1 Like