Articles → LIGHT SWITCH 2011 → Global Variables In Lightswitch 2011

Global Variables In Lightswitch 2011






Software Requirement




  1. Visual studio 2010 is installed on your machine.
  2. Visual Studio 2010 service pack 1 is installed on your machine.
  3. Microsoft Visual Studio Light switch 2011 is installed on your machine.

Prerequisite Knowledge




  1. Basics about lightswitch.
  2. How to create screens in lightswitch?
  3. What is Application class in lightswitch?
  4. What is the purpose of application.cs file?

Steps Of Execution




  1. Create a new project
  2. Add screens
  3. Add global variable in application.cs
  4. Set global variable in screen 1
  5. Get global variable in screen 2



Create A New Project




Picture showing the folder structure of the lightswitch project in the solution explorer

Click to Enlarge


Add Screens




Picture showing 2 screens added in the lightswitch project in the solution explorer

Click to Enlarge


Add Global Variable In Application.Cs




  1. Right click on the project and click ‘Properties’.
  2. From the ‘Screen Navigation’ tab click on the link ‘Click here to view application code’
  3. Picture showing the Click here to view application code link in the Screen Navigation tab

    Click to Enlarge

  4. Add a property of type string (can of any data type depends upon the requirement). See the code of application.cs file below
  5. namespace LightSwitchApplication {
    	public partial class Application {
    		public string MyGlobalVariable {
    			get;
    			set;
    		}
    	}
    }



Set Global Variable In Screen 1




namespace LightSwitchApplication {
	public partial class Screen1 {
		partial void Screen1_Created() {
			// Write your code here.
			Application.MyGlobalVariable = "Global Variable Set in Screen 1";
			Application.ShowScreen2();
		}
	}
}





Get Global Variable In Screen 2




using Microsoft.LightSwitch.Presentation.Extensions;

namespace LightSwitchApplication {
	public partial class Screen2 {
		partial void Screen2_Created() {
			this.ShowMessageBox(Application.MyGlobalVariable);
		}
	}
}





Output


Picture showing the output of the Global variables in lightswitch 2011

Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Tuesday, March 26, 2013

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250