Main Site Documentation

Question about scope


i would like the variable xyz to be avaialble at all levels, but this does not appear to be happening, even though it is defined in a public section…what is wrong here? I get the error: the name xyz does not exist not in this context …is there a fix?

    public class Program
        // This will hold the main window.
        static Window window;

        public static void Main()
            string xyz = "";

        static void btn_Releaseit(object sender)
           xyz = "testing";     //error the name xyz does not exist not in this context 

        static void btn_PressIt(object sender)
           xyz = "here it is";     //error the name xyz does not exist not in this context 

         static void InitWin()


You should define

static string xyz;

just below the declaration of “window”.


When a variable is defined inside method, the body of the method is the scope of that variable. Not only it is not visible out of scope it will also be collected after the method exits.


That did the trick!!..
I had already tried plain: string xyz below “window”…adding the word static fixed everything up…how come?

what is the difference between (when it come to the lower level routines)

static string xyz=“hello”;
string xyz= “hello”;


This is because Main is the static method it only has access to the static variables of the class. Non static members are created when an instance of the class is created.

public static string xyzStatic="hello"; 
public string xyzNonStatic= "hello";

So for the variable below you can do this

Debug.Print( "Static member" + Program.xyzStatic);

Program instanceOfTheProgramClass = new Program();
Debug.Print( "Non static member" + instanceOfTheProgramClass.xyzNonStatic);


Please see :
or :

Better reading from the source :wink: