none
Global variable

    Question

  • Does LS have global variables? Where do i set it? For examples when a user log in i want to tag some properties or info to him that i can use as parameter for my query throughout the apps for the session. For example, the user log in and select a particular division, then my apps screens will filter data base on the division selected instead of him repeatedly entering division in every screens.  
    Saturday, July 30, 2011 5:04 AM

Answers

  • Hi!

    Yes - LS supports global variables. You can achieve a solution by declaring a new member to the Application class.

      public partial class Application
      {
        public string CurrentDepartment { get; set; }
    
      }
    


    In the above shown example, I am adding a member called CurrentDepartment which can now be accessed from anywhere in your LS application by using the following code snippet:

     

    this.Application.CurrentDepartment = "the current department";
    


    Best regards,

    Wolfgang


    -- wolfgang strasser, software architect w.strasser_AT_cubido.at
    Saturday, July 30, 2011 8:16 AM

All replies

  • Hi!

    Yes - LS supports global variables. You can achieve a solution by declaring a new member to the Application class.

      public partial class Application
      {
        public string CurrentDepartment { get; set; }
    
      }
    


    In the above shown example, I am adding a member called CurrentDepartment which can now be accessed from anywhere in your LS application by using the following code snippet:

     

    this.Application.CurrentDepartment = "the current department";
    


    Best regards,

    Wolfgang


    -- wolfgang strasser, software architect w.strasser_AT_cubido.at
    Saturday, July 30, 2011 8:16 AM
  • Thank you Wolfgang. I am still new to writing codes, so mind telling which program to put in that few lines of codes to declare the global variable in the application class? 
    Saturday, July 30, 2011 8:42 AM
  • An easy way to switch to the code of the Application class is the following

    -> right click on the LS Project -> properties

    -> Screen navigation and here you see a link indicating "Click here to view application code"


    -- wolfgang strasser, software architect w.strasser_AT_cubido.at
    Saturday, July 30, 2011 8:58 AM
  • Thank you. Where did you learn all this? I really need to learn more about coding.
    • Proposed as answer by LuLuInCO Thursday, August 16, 2012 9:55 PM
    Saturday, July 30, 2011 9:21 AM
  • Thank you Wolfgang.

    But where do I store the global session variables which I need to use on the server side: for queries, _Inserting, etc.?


    Artem Loshak PCLP
    Sunday, September 25, 2011 4:26 AM
  • Hi!

     

    You could place the variable into the DataService class:

     

        public partial class MyDataService
        {
            public string ServerGlobalVariable { get; set; }
    
             partial void Test_Inserted(Test entity)
            {
                this.ServerGlobalVariable = "some value";
            }
        }
    

    -> The usage is the same as for variables placed into the Application class.

     

    cheers,

    Wolfgang


    -- wolfgang strasser, software architect w.strasser_AT_cubido.at
    Sunday, December 18, 2011 7:11 PM
  • Where is the DataService Class located?

    Yann had mentioned in a different post http://social.msdn.microsoft.com/Forums/en/lightswitchgeneral/thread/a9a5f8ba-1269-4f29-b293-987798b3432e  to put it a class in the Common section and you can get to it from sever or client side, but I am running into a permission issue when I make a call to a Class in the Common section from the Application_Initialize command, because the user has not been Authenticated, would your suggestion to put this in the DataService Class work in this scenario 

    Wednesday, December 21, 2011 6:43 PM
  • I have tried putting the variable

    public Organization CurrentOrganization {get; set: }

    in both my Application.cs code and my DataService.cs code but I cannot reference it from elsewhere in my code. I get an error "'LightSwitchApplication.DataWorkspace' does not contain a definition for 'CurrentOrganization' and no extension method 'CurrentOrganization' accepting a first argument of type 'LightSwitchApplication.DataWorkspace' could be found (are you missing a using directive or an assemby reference?)"

    I've tried this.Application.CurrentOrganization and this.DataWorkspace.CurrentOrganization but both fail. Organization is an entity I have retrieved based on the current logged-in User.

    Any idea what I am doing wrong? Thanks.

     

    Monday, March 5, 2012 10:33 PM
  • There is no "global" variable in LS.  There are separate "global" variables in the Application on the Client side and DataService for the Server side.

    I'm waiting on documentation that explains how the architecture has changed with V2.


    Garth Henderson - Vanguard Business Technology

    Tuesday, March 6, 2012 7:10 PM
  • Try public static Organization CurrentOrganization {get; set: }
    Wednesday, May 16, 2012 9:04 PM
  • You can share constant var in Common which is loaded on both sides.  However, you can not share changing variabled across machines as that would involve remoting changes and sync issues, etc.  Share common vars across tiers in the DB.  Can use a Settings table or something with a single row.  No magic in LS here.  Think of two seperate call stacks and app domains on two seperate machines.  Also, I would not count on sharing static vars on the server side either.  What happens when another app pool spins up or another request on another web server?  You end up doing session vars on the DB again as the broker.

    Wednesday, May 16, 2012 9:36 PM
  • Hi Wolfgang

    Many thanks for your solution - it save me a lot of hassle.

    If I could vote a + for you 1,000 times I would ;-)

    All the best, Mark.


    Mark

    Thursday, May 17, 2012 2:59 PM