locked
Scope and Lifetime of .Net Variables in RDL Embedded Custom Code RRS feed

  • Question

  • Morning all,

    I have seen several examples on the web of developers declaring and assigning variables in the .Net custom code that is embedded within their RDL.  The variables I'm talking about are outside the scope of the functions themselves but within the RDL <Code> element.  I'm very confused about the scope and lifetime of these variables.  I haven't found good documentation about how they are supposed to work.

    In particular, I'd like to understand what the lifetime of a one of these variable should be without the "Shared" keyword.


    In the FAQ there is an example that uses "Shared" variables in custom code (see Q. How to Reset the page number...)
    http://social.msdn.microsoft.com/Forums/en-US/sqlreportingservices/thread/48de91f9-1844-40c1-9614-5ead0b4b69a5

    However, in this SQL2008 blog entry there is a similar example of using .Net variables but without the "Shared" keyword.
    http://blogs.msdn.com/robertbruckner/archive/2008/07/20/Using-group-variables-in-reporting-services-2008-for-custom-aggregation.aspx

    So my question is if I declare and assign data to a .Net variable that does NOT have the "Shared" keyword, what is the scope and lifetime of it?  Should I be able to reference it on every page of my report?  Why do some examples use the "Shared" keyword and some do not?  Is there any documentation on this?


    Any help would be very much appreciated.
    Thanks, David



    David Beavon
    Wednesday, March 17, 2010 2:07 PM

Answers

  • Hi David,

    A variable declared with the Shared modifier is a shared variable. A shared variable identifies exactly one storage location regardless of the number of instances of the type that are created. A shared variable comes into existence when a program begins executing, and ceases to exist when the program terminates.

    In SQL Server Reporting Services, the Shared variable's lifetime is same to the command variable.

    This should be a question of VB.NET, I would suggest you reading this article for more information:

    http://msdn.microsoft.com/en-us/library/aa711968(VS.71).aspx

    Thanks,

    Jin Chen


    Jin Chen - MSFT
    Friday, March 19, 2010 12:26 PM

All replies

  • Hi David,

    A variable declared with the Shared modifier is a shared variable. A shared variable identifies exactly one storage location regardless of the number of instances of the type that are created. A shared variable comes into existence when a program begins executing, and ceases to exist when the program terminates.

    In SQL Server Reporting Services, the Shared variable's lifetime is same to the command variable.

    This should be a question of VB.NET, I would suggest you reading this article for more information:

    http://msdn.microsoft.com/en-us/library/aa711968(VS.71).aspx

    Thanks,

    Jin Chen


    Jin Chen - MSFT
    Friday, March 19, 2010 12:26 PM
  • Hi Jin,

        I'm having an issue with referencing a variable within custom code.  I have a function that is calculating a running total and it works fine when executed within a detail of a grouping but when I go to display the total in the group footer,  the total has a value of 0.  It seems the variable is out of scope or is reset when referenced from the footer.  I can't just use any of the built-in functions to just add up the column total because I'm adding up amounts based on over-lapping date ranges.

    If I can't get this to work, I will have to completely bail on using BIDS and I don't want to do that.

    I have seen other posts with folks having a similiar issue but I don't see any solution.

    I'm using BIDS 2005 and SQL Server 2005.

    Thanks

    Shelby


    Shelby
    Thursday, March 31, 2011 1:20 PM
  • I did something similar here

    http://road-blogs.blogspot.com/2011/02/display-current-page-total-in-footer.html

     

    Using custom code in footer and difference was to make the variable shared, I think.


    Cheers,
    Jason
    P.S. : Please click the 'Mark as Answer' button if a post solves your problem! :)

    Some Random Thoughts

    Thursday, March 31, 2011 3:13 PM