Articles → LIGHT SWITCH 2011 → Global Variables In Lightswitch 2011
Global Variables In Lightswitch 2011
Software Requirement
- Visual studio 2010 is installed on your machine.
- Visual Studio 2010 service pack 1 is installed on your machine.
- Microsoft Visual Studio Light switch 2011 is installed on your machine.
Prerequisite Knowledge
- Basics about lightswitch.
- How to create screens in lightswitch?
- What is Application class in lightswitch?
- What is the purpose of application.cs file?
Steps Of Execution
- Create a new project
- Add screens
- Add global variable in application.cs
- Set global variable in screen 1
- Get global variable in screen 2
Create A New Project
Click to Enlarge
Add Screens
Click to Enlarge
Add Global Variable In Application.Cs
- Right click on the project and click ‘Properties’.
- From the ‘Screen Navigation’ tab click on the link ‘Click here to view application code’
Click to Enlarge
- Add a property of type string (can of any data type depends upon the requirement). See the code of application.cs file below
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
Click to Enlarge